Learn SQL: Queries

Perform more complex queries by learning essential query keywords and functionality

Start[missing "en.views.course_landing_page.learn-sql.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

SELECT statement in SQL

SELECT * FROM movies;

The SELECT * statement in SQL returns all columns from the provided table(s) in the result set. The query above will select all columns and records from the movies table.

Setting an alias to columns or tables

SELECT * FROM movies;

Columns or tables in SQL can be aliased using the AS clause. This allows columns or tables to be specifically renamed in the returned result set. The query above will return a result set with the column for name renamed to movie_title.

DISTINCT query in SQL

SELECT * FROM movies;

Unique values of a column can be selected using a DISTINCT query. For a table contact_details having five rows in which the city column contains Chicago, Madison, Boston, Madison, and Denver, the query above would return:

  • Chicago
  • Madison
  • Boston
  • Denver

WHERE clause in SQL

SELECT * FROM movies;

The SQL WHERE clause is used to filter records that match a certain condition. The query above will match all records where the pub_year equals 2017.

LIKE operator in SQL

SELECT * FROM movies;

The SQL LIKE operator can be used inside of a WHERE clause to match a specified pattern. The query above will match any movie that begins with Star in its title.

SQL _ wildcard for single unspecified characters

SELECT * FROM movies;

The SQL _ wildcard can be used in LIKE operator pattern to match any single unspecified character. The query above will match any movie which begins with a single character, followed by ove.

SQL % wildcard for zero or more unspecified character(s)

SELECT * FROM movies;

The SQL % wildcard can be used in LIKE operator pattern to match zero or more unspecified character(s). The query above will match any movie that begins with The, followed by zero or more characters.

NULL column values in SQL

SELECT * FROM movies;

Column values in SQL records can be NULL, or no value. These records can be matched (or not matched) using the IS NULL and IS NOT NULL operators in combination with the WHERE clause. The query above will match all addresses where the address has a value, or is not NULL.

BETWEEN operator in SQL

SELECT * FROM movies;

The SQL BETWEEN operator can be used to filter by a range of values. The range values can be text, numbers or date data. The query above will match any movie made between the years 1980 and 1990, inclusive.

AND operator in SQL

SELECT * FROM movies;

The SQL AND operator allows multiple conditions to be combined. Records must match both conditions that are joined by AND and be included in a result set. The query above will match any car that is blue and was made after 2014.

OR operator in SQL

SELECT * FROM movies;

The SQL OR operator allows multiple conditions to be combined. Records matching either condition joined by the OR are included in the result set. The query above will match customers whose state is either ca or ny.

ORDER BY clause in SQL

SELECT * FROM movies;

The ORDER BY clause can be used to sort the result set of a query by one or more columns. Using the ORDER BY clause, data can be ordered in ascending (default) or descending order by the ASC and DESC keywords. In the example, all the rows of the contacts table will be ordered by birth_date column in descending order.

LIMIT clause in SQL

SELECT * FROM movies;

The SQL LIMIT clause is used to narrow, or limit, a result set to the specified number of rows. The query above will limit the result set to 5 rows.

Queries
Lesson 1 of 1
  1. 1

    In this lesson, we will be learning different SQL commands to query a single table in a database. One of the core purposes of the SQL language is to retrieve information stored in a database. ...

  2. 2

    Previously, we learned that [...] is used every time you want to query data from a database and [...] means all columns. Suppose we are only interested in two of the columns. We can select i...

  3. 3

    Knowing how [...] works, suppose we have the code below: [...] Can you guess what [...] does? [...] is a keyword in SQL that allows you to rename a column or table using an alias. The ...

  4. 4

    When we are examining data in a table, it can be helpful to know what distinct values exist in a particular column. [...] is used to return unique values in the output. It filters out all dupl...

  5. 5

    We can restrict our query results using the [...] clause in order to obtain only the information we want. Following this format, the statement below filters the result set to only include top ra...

  6. 6

    [...] can be a useful operator when you want to compare similar values. The [...] table contains two films with similar titles, 'Se7en' and 'Seven'. How could we select all movies that sta...

  7. 7

    The percentage sign [...] is another wildcard character that can be used with [...] . This statement below filters the result set to only include movies with names that begin with the letter 'A...

  8. 8

    By this point of the lesson, you might have noticed that there are a few missing values in the [...] table. More often than not, the data you encounter will have missing values. Unknown values ...

  9. 9

    The [...] operator can be used in a [...] clause to filter the result set within a certain range. The values can be numbers, text or dates. This statement filters the result set to only incl...

  10. 10

    Sometimes we want to combine multiple conditions in a [...] clause to make the result set more specific and useful. One way of doing this is to use the [...] operator. Here, we use the [.....

  11. 11

    Similar to [...] , the [...] operator can also be used to combine multiple conditions in [...] , but there is a fundamental difference: - [...] operator displays a row if all the conditio...

  12. 12

    That's it with [...] and its operators. Moving on! It is often useful to list the data in our result set in a particular order. We can sort the results using [...] , either alphabetically o...

  13. 13

    We've been working with a fairly small table (fewer than 250 rows), but most SQL tables contain hundreds of thousands of records. In those situations, it becomes important to cap the number of rows...

  14. 14

    A [...] statement allows us to create different outputs (usually in the [...] statement). It is SQL's way of handling if-then logic. Suppose we want to condense the ratings in [...] to thr...

  15. 15

    Congratulations! We just learned how to query data from a database using SQL. We also learned how to filter queries to make the information more specific and useful. Let's summarize: - [...] ...

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo

Learn SQL: Queries

Start[missing "en.views.course_landing_page.learn-sql.course_illustration" translation]