SELECT * statement returns all columns from the provided table in the result set. The given query will fetch all columns and records (rows) from the
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 given query will return a result set with the column for
name renamed to
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 given query would return:
WHERE clause is used to filter records (rows) that match a certain condition. The given query will select all records where the
LIKE operator can be used inside of a
WHERE clause to match a specified pattern. The given query will match any movie that begins with
Star in its title.
_ wildcard can be used in a
LIKE operator pattern to match any single unspecified character. The given query will match any movie which begins with a single character, followed by
% wildcard can be used in a
LIKE operator pattern to match zero or more unspecified character(s). The example query will match any movie that begins with
The, followed by zero or more of any characters.
Column values in SQL records can be
NULL, or have 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 given query will match all addresses where the address has a value or is not
BETWEEN operator can be used to filter by a range of values. The range of values can be text, numbers or date data. The given query will match any movie made between the years 1980 and 1990, inclusive.
AND operator allows multiple conditions to be combined. Records must match both conditions that are joined by
AND to be included in the result set. The example query will match any car that is blue and made after 2014.
OR operator allows multiple conditions to be combined. Records matching either condition joined by the
OR are included in the result set. The given query will match customers whose state is either
ORDER BY Clause
ORDER BY Clause
ORDER BY clause can be used to sort the result set by a particular column either alphabetically or numerically. It can be ordered in ascending (default) or descending order with
DESC. In the example, all the rows of the
contacts table will be ordered by the
birth_date column in descending order.
LIMIT clause is used to narrow, or limit, a result set to the specified number of rows. The given query will limit the result set to 5 rows.
- 1In 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. …
- 2Previously, we learned that SELECT 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 individu…
- 3Knowing how SELECT works, suppose we have the code below: SELECT name AS ‘Titles’ FROM movies; Can you guess what AS does? AS is a keyword in SQL that allows you to rename a column or table u…
- 4When we are examining data in a table, it can be helpful to know what distinct values exist in a particular column. DISTINCT is used to return unique values in the output. It filters out all dup…
- 5We can restrict our query results using the WHERE clause in order to obtain only the information we want. Following this format, the statement below filters the result set to only include top rate…
- 6LIKE can be a useful operator when you want to compare similar values. The movies table contains two films with similar titles, ‘Se7en’ and ‘Seven’. How could we select all movies that start w…
- 7The percentage sign % is another wildcard character that can be used with LIKE. This statement below filters the result set to only include movies with names that begin with the letter ‘A’: SELEC…
- 8By this point of the lesson, you might have noticed that there are a few missing values in the movies table. More often than not, the data you encounter will have missing values. Unknown values a…
- 9The BETWEEN operator is used in a WHERE clause to filter the result set within a certain range. It accepts two values that are either numbers, text or dates. For example, this statement filters …
- 10Sometimes we want to combine multiple conditions in a WHERE clause to make the result set more specific and useful. One way of doing this is to use the AND operator. Here, we use the AND operat…
- 11Similar to AND, the OR operator can also be used to combine multiple conditions in WHERE, but there is a fundamental difference: - AND operator displays a row if all the conditions are true. - …
- 12That’s it with WHERE 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 ORDER BY, either alphabetically or…
- 13We’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…
- 14A CASE statement allows us to create different outputs (usually in the SELECT statement). It is SQL’s way of handling if-then logic. Suppose we want to condense the ratings in movies to three le…
- 15Congratulations! 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: - SELECT i…