SQL Lesson 1 - SELECT queries 101

To retrieve data from a SQL database, we need to writeĀ SELECTĀ statements, which are often colloquially referred to asĀ queries. A query in itself is just a statement which declares what data we are looking for, where to find it in the database, and optionally, how to transform it before it is returned. It has a specific syntax though, which is what we are going to learn in the following exercises.

As we mentioned in the introduction, you can think of a table in SQL as a type of an entity (ie. Dogs), and each row in that table as a specificĀ instanceĀ of that type (ie. A pug, a beagle, a different colored pug, etc). This means that the columns would then represent the common properties shared by all instances of that entity (ie. Color of fur, length of tail, etc).

And given a table of data, the most basic query we could write would be one that selects for a couple columns (properties) of the table with all the rows (instances).

Select query for a specific columns

SELECT column, another_column, … FROM mytable;

The result of this query will be a two-dimensional set of rows and columns, effectively a copy of the table, but only with the columns that we requested.

If we want to retrieve absolutely all the columns of data from a table, we can then use the asterisk (*) shorthand in place of listing all the column names individually.

Select query for all columns

SELECT * FROM mytable;

This query, in particular, is really useful because it's a simple way to inspect a table by dumping all the data at once.

Exercise

We will be using a database with data about some of Pixar's classic movies for most of our exercises. This first exercise will only involve theĀ MoviesĀ table, and the default query below currently shows all the properties of each movie. To continue onto the next lesson, alter the query to find the exact information we need for each task.

1. Find theĀ `title`Ā of each film
	1. SELECT Title FROM movies;
2. Find theĀ `director`Ā of each film
	1. SELECT Director FROM movies;
3. Find theĀ `title`Ā andĀ `director`Ā of each film
	1. SELECT Title, Director FROM movies;
4. Find theĀ `title`Ā andĀ `year`Ā of each film
	1. SELECT Title, Year FROM movies;
5. FindĀ `all`Ā the information about each film
	1. SELECT * FROM movies;

Exercise 1 — Tasks

1. Find theĀ `title`Ā of each filmĀ āœ“
2. Find theĀ `director`Ā of each filmĀ āœ“
3. Find theĀ `title`Ā andĀ `director`Ā of each filmĀ āœ“
4. Find theĀ `title`Ā andĀ `year`Ā of each filmĀ āœ“
5. FindĀ `all`Ā the information about each filmĀ āœ“

The current webpage is "SQL Lesson 1: SELECT queries 101" from SQLBolt. It introduces the basics of retrieving data from a SQL database using the SELECT statement, which defines what data to fetch, from which table, and optionally how to transform it.

Key points covered:

Exercises at the end ask you to write SELECT queries to:

  1. Find the title of each film
  2. Find the director of each film
  3. Find both the title and director of each film
  4. Find both the title and year of each film
  5. Retrieve all information about each film

This lesson is designed as an interactive introduction to the SELECT statement in SQL, with practice tasks to reinforce learning.

1