SQL

SELECT with WHERE / ORDER BY / LIMIT

admin by @admin ADMIN
5d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
The four-clause workhorse: pick columns, filter rows, sort them, return the top N. Standard across every database.
SQL
Raw
-- 20 most recent active users from Texas, oldest signup first
SELECT id,
       name,
       email,
       created_at
FROM   users
WHERE  state = 'TX'
   AND status = 'active'
   AND created_at >= NOW() - INTERVAL '30 days'
ORDER  BY created_at ASC
LIMIT  20;

-- Tip: prefer the column LIST over SELECT *  — it makes the query
-- forward-compatible (a new column won't change your output shape),
-- documents intent, and is faster when wide tables have unused BLOBs.
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.