Skip to main content
KodeKloud’s MeowTube is growing fast. Videos, users, and comments are accumulating into a large, structured dataset. After weighing relational vs non-relational options, KodeKloud chose a relational database for MeowTube — it’s predictable, reliable, and well suited to the kinds of queries the app needs. But as the dataset grows, simple SELECTs start taking longer. Understanding SQL query structure and how to optimize it (including using indexes) is essential for DevOps and cloud engineers to keep applications responsive at scale. This lesson builds on CRUD basics and shows how to:
  • 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
We’ll use the 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, use SELECT * FROM table_name; — don’t forget the semicolon.

Selecting specific columns

If you only need certain columns, list them instead of *:
Return multiple columns by separating them with commas:

Filtering rows with WHERE

Use WHERE to restrict rows. Column names must match the table schema (here upload_date):
Tip: If you press Enter without a terminating semicolon, the MySQL prompt shows -> and waits for the statement to be completed. This makes multi-line queries easier to read.

Sorting with ORDER BY

Sort results using ORDER BY. Use DESC for descending (newest first) and ASC for ascending:

Limit the result set with LIMIT

Use LIMIT 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 use JOIN. 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:
When columns appear in more than one table, use explicit table qualification (dot notation) like 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.

Watch Video