KevsRobots Learning Platform
42% Percent Complete
By Kevin McAleer, 6 Minutes
In this lesson, we’ll cover the SELECT statement, which is the cornerstone of querying data in SQL. You’ll learn how to retrieve data from tables, apply basic filters, and sort your results.
SELECTThe SELECT statement is used to retrieve specific data from one or more tables. You can use SELECT to display specific columns, filter rows, sort results, and perform calculations.
SELECT SyntaxSELECT column1, column2, ...
FROM table_name;
Example: Display all columns from the users table.
SELECT * FROM users;
Example: Display only the name and email columns.
SELECT name, email FROM users;
The * symbol selects all columns in the table, while specifying individual columns limits the output to just those columns.
WHEREThe WHERE clause allows you to filter rows based on specified conditions. It’s used to retrieve only the data that meets certain criteria.
WHERE SyntaxSELECT column1, column2, ...
FROM table_name
WHERE condition;
Example: Retrieve all users with the name “Alice”.
SELECT * FROM users
WHERE name = 'Alice';
Example: Retrieve users who are 30 years old or older.
SELECT * FROM users
WHERE age >= 30;
WHERE| Operator | Description | Example |
|---|---|---|
= |
Equal to | WHERE age = 30 |
<> or != |
Not equal to | WHERE name != 'Alice' |
< |
Less than | WHERE age < 25 |
<= |
Less than or equal to | WHERE age <= 30 |
> |
Greater than | WHERE age > 25 |
>= |
Greater than or equal to | WHERE age >= 30 |
BETWEEN |
Within a range | WHERE age BETWEEN 20 AND 30 |
LIKE |
Pattern matching | WHERE name LIKE 'A%' |
IN |
Matches any value in a list | WHERE age IN (25, 30, 35) |
Tip: Use
LIKEwith wildcards (%and_) for flexible pattern matching.%matches any sequence of characters, while_matches a single character.
ORDER BYThe ORDER BY clause sorts the result set by one or more columns in ascending (ASC) or descending (DESC) order.
ORDER BY SyntaxSELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...;
Example: List all users, sorted by age in ascending order.
SELECT * FROM users
ORDER BY age ASC;
Example: List all users, sorted by age in descending order.
SELECT * FROM users
ORDER BY age DESC;
Example: Sort by age, and then by name within each age group.
SELECT * FROM users
ORDER BY age ASC, name ASC;
LIMITThe LIMIT clause restricts the number of rows returned by the query. This is especially useful when testing queries or when only the top results are needed.
LIMIT SyntaxSELECT column1, column2, ...
FROM table_name
LIMIT number_of_rows;
Example: Retrieve the first 5 users.
SELECT * FROM users
LIMIT 5;
LIMIT with ORDER BYLIMIT can be used with ORDER BY to retrieve the top or bottom values.
Example: Retrieve the 3 oldest users.
SELECT * FROM users
ORDER BY age DESC
LIMIT 3;
AND and ORYou can combine multiple conditions in the WHERE clause using AND and OR operators to refine your queries.
AND: All conditions must be true.OR: At least one condition must be true.ANDExample: Retrieve users who are over 25 years old and whose name starts with “A”.
SELECT * FROM users
WHERE age > 25 AND name LIKE 'A%';
ORExample: Retrieve users whose name is “Alice” or who are 30 years old.
SELECT * FROM users
WHERE name = 'Alice' OR age = 30;
ASAliases give temporary names to columns or tables for easier readability. They do not change the actual column or table names in the database.
Example: Display user names with the alias “User Name”.
SELECT name AS "User Name"
FROM users;
Example: Use an alias for the users table to shorten query syntax.
SELECT u.name, u.age
FROM users AS u
WHERE u.age > 25;
SELECT QueriesRetrieve all users from the users table, displaying only the name and email columns.
SELECT name, email FROM users;
Retrieve all users whose age is between 20 and 30.
SELECT * FROM users
WHERE age BETWEEN 20 AND 30;
Retrieve the top 3 users by age, in descending order.
SELECT * FROM users
ORDER BY age DESC
LIMIT 3;
Retrieve users whose name starts with “A” and display their names as “User”.
SELECT name AS "User" FROM users
WHERE name LIKE 'A%';
SELECT Query TechniquesHere’s a recap of the techniques covered in this lesson:
| Command | Description | Example |
|---|---|---|
SELECT |
Retrieves data from a table | SELECT * FROM users; |
WHERE |
Filters results based on conditions | WHERE age > 25 |
ORDER BY |
Sorts results by specified columns | ORDER BY age DESC |
LIMIT |
Limits the number of rows returned | LIMIT 5 |
AND / OR |
Combines multiple conditions | WHERE age > 25 AND name LIKE 'A%' |
AS |
Provides an alias for columns or tables | SELECT name AS "User Name" |
With these querying techniques, you’re now equipped to retrieve and filter data from your tables effectively. In the next lesson, we’ll expand on filtering techniques and introduce conditional logic for more powerful queries.
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments