- Construct common SELECT queries correctly
- Use filtering, sorting, limiting, and joins
- Understand clause order and why it matters
- Prepare for the next topic: indexes and performance
users and videos tables as examples. Pay attention to the order of clauses — SQL expects them in a specific sequence, similar to following a recipe.
Inspecting whole tables
To return every column and every row from a table, useSELECT * FROM table_name; — don’t forget the semicolon.
Selecting specific columns
If you only need certain columns, list them instead of*:
Filtering rows with WHERE
UseWHERE to restrict rows. Column names must match the table schema (here upload_date):
-> and waits for the statement to be completed. This makes multi-line queries easier to read.
Sorting with ORDER BY
Sort results usingORDER BY. Use DESC for descending (newest first) and ASC for ascending:
Limit the result set with LIMIT
UseLIMIT to restrict how many rows are returned. LIMIT should come last in the statement:
Combining tables with JOIN
To combine related data from multiple tables useJOIN. JOIN appears immediately after FROM and defines how rows from each table match. In this example we join videos to users on the shared user_id, then sort and limit the result:
videos.user_id and users.user_id to avoid ambiguity.
Clause order matters:
FROM (and JOIN) determine which tables are involved, WHERE filters rows, ORDER BY sorts them, and LIMIT trims the final result set. Writing queries in the correct sequence helps the database optimize execution.Typical clause order and purpose
Use the table below as a quick reference for the common SELECT clause sequence and what each clause does.
These patterns underpin searching, filtering, sorting feeds, and generating recommendations. As data scales, even well-formed queries can slow down — that’s when indexes become essential for performance. In the next part of this lesson we’ll cover what a database index is, how it speeds up queries, and when to add one.