Database and SQL Starter · free course

Free course · Lesson 2 of 5

Querying & Filtering Data

In this lesson you will

  • Filter rows with WHERE
  • Sort and limit results

The SELECT statement is the most common SQL command. You can filter rows with WHERE, sort with ORDER BY, and limit results with LIMIT (MySQL/PostgreSQL) or TOP (SQL Server). Filtering is essential for generating reports and finding specific records in large datasets.

  • SELECT name, program FROM students WHERE age > 21; — filters students older than 21.
  • ORDER BY age DESC — sorts from oldest to youngest.
  • LIMIT 3 — returns only the first 3 rows.
  • Use LIKE for pattern matching: WHERE city LIKE 'J%' finds cities starting with "J".

💡 Practical Skill: Use MySQL Workbench or pgAdmin to visually build queries. Export results to CSV for reports. Track your schema changes with Git by saving SQL scripts as .sql files.

Worked exampleFiltering & Sorting Records

SQL (MySQL / PostgreSQL)

SQL
-- Find all students from JubaSELECT name, program, ageFROM studentsWHERE city = 'Juba'; -- Sort students by age (youngest first)SELECT name, age, cityFROM studentsORDER BY age ASC; -- Find the top 2 oldest studentsSELECT name, age, programFROM studentsORDER BY age DESCLIMIT 2; -- Pattern search: students whose name starts with 'G'SELECT * FROM studentsWHERE name LIKE 'G%';-- Output: Grace, 22, Juba, Database Management

Practice exercise

From a table of clinic visits, list the ten most recent visits for one diagnosis, newest first.

Key takeaways

  • WHERE selects rows; ORDER BY sorts them; LIMIT takes the top
  • Use IS NULL, not = NULL, for missing values
  • Use parentheses when mixing AND and OR
In the full program Module 2: Filtering and Sorting with SQL See how this topic is taught in depth, with practical activities and an assessment.

Free self-study lesson — no certificate. For the full instructor-led program, see registration options.