114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
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.
SELECT
The 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 column1, column2, ... FROM table_name;
Example: Display all columns from the users table.
users
SELECT * FROM users;
Example: Display only the name and email columns.
name
email
SELECT name, email FROM users;
The * symbol selects all columns in the table, while specifying individual columns limits the output to just those columns.
*
WHERE
The WHERE clause allows you to filter rows based on specified conditions. It’s used to retrieve only the data that meets certain criteria.
SELECT 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 age = 30
<>
!=
WHERE name != 'Alice'
<
WHERE age < 25
<=
WHERE age <= 30
>
WHERE age > 25
>=
WHERE age >= 30
BETWEEN
WHERE age BETWEEN 20 AND 30
LIKE
WHERE name LIKE 'A%'
IN
WHERE age IN (25, 30, 35)
Tip: Use LIKE with wildcards (% and _) for flexible pattern matching. % matches any sequence of characters, while _ matches a single character.
%
_
ORDER BY
The ORDER BY clause sorts the result set by one or more columns in ascending (ASC) or descending (DESC) order.
ASC
DESC
SELECT 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;
LIMIT
The 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.
SELECT column1, column2, ... FROM table_name LIMIT number_of_rows;
Example: Retrieve the first 5 users.
SELECT * FROM users LIMIT 5;
LIMIT 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
OR
You can combine multiple conditions in the WHERE clause using AND and OR operators to refine your queries.
Example: Retrieve users who are over 25 years old and whose name starts with “A”.
SELECT * FROM users WHERE age > 25 AND name LIKE 'A%';
Example: Retrieve users whose name is “Alice” or who are 30 years old.
SELECT * FROM users WHERE name = 'Alice' OR age = 30;
AS
Aliases 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;
Retrieve all users from the users table, displaying only the name and email columns.
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.
Retrieve users whose name starts with “A” and display their names as “User”.
SELECT name AS "User" FROM users WHERE name LIKE 'A%';
Here’s a recap of the techniques covered in this lesson:
ORDER BY age DESC
LIMIT 5
WHERE age > 25 AND name LIKE 'A%'
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.
< Previous Next >