Skip to content
SQL

SELECT with WHERE and ORDER BY

Filter, sort, and limit rows with SELECT, WHERE, and ORDER BY in SQL.

By EZ4Code Team
selectquerybeginner

Code

-- Basic SELECT with filtering and sorting
SELECT id, name, email, created_at
FROM users
WHERE status = 'active'
  AND age >= 18
ORDER BY created_at DESC
LIMIT 10 OFFSET 20;

-- Select distinct values
SELECT DISTINCT country
FROM users
ORDER BY country;

-- Column aliases and expressions
SELECT
  name,
  price * 1.2 AS price_with_tax,
  UPPER(name) AS name_upper
FROM products;

Explanation

Filters rows with WHERE, sorts with ORDER BY, and limits results with LIMIT/OFFSET for pagination. DISTINCT removes duplicate rows, while column aliases and expressions allow computed output like price_with_tax. This is the foundation of most read queries in SQL.

More SQL Snippets