SQL Basics: Talking to Databases
Ask questions. Get answers. In one line.
Ask questions. Get answers. In one line.
SQL reads like English: "SELECT name FROM students WHERE grade > 80." Click each query to run it against the sample data and see the results.
Type a SQL query and run it. Try SELECT, WHERE, ORDER BY, and LIMIT. The table has columns: id, name, age, grade, sport.
Every database application does four things: Create (INSERT), Read (SELECT), Update (UPDATE), Delete (DELETE). Together they are called CRUD.
INSERT INTO students (name, age, grade) VALUES ("New Student", 15, 90). Adds a new row to the table.
SELECT * FROM students WHERE id = 5. Reads data without changing it. The most common operation by far.
UPDATE students SET grade = 95 WHERE name = "Alice". Changes existing data. Always use WHERE or you update every row.
DELETE FROM students WHERE grade < 50. Removes rows. Always use WHERE or you delete everything. There is no undo.
Nearly every application with persistent data uses SQL under the hood.
SELECT * FROM posts WHERE user_id IN (SELECT following_id FROM follows WHERE user_id = me) ORDER BY created_at DESC. That is your feed.
SELECT * FROM products WHERE category = "shoes" AND price < 100 ORDER BY rating DESC LIMIT 20. Search, filter, sort, paginate.
SELECT country, COUNT(*) as users FROM signups GROUP BY country ORDER BY users DESC. One query, instant insights.
SQL was invented at IBM in 1970 by Edgar Codd. It has been the standard database language for over 50 years. Despite dozens of "NoSQL" alternatives, SQL databases still power the vast majority of the world's data. MySQL, PostgreSQL, and SQLite are used by virtually every tech company.
You've written real SQL queries: SELECT to read, WHERE to filter, ORDER BY to sort, and INSERT to add data. Every app with a database uses these exact commands millions of times per day.
SELECT name, age FROM users returns just those columns. SELECT * returns everything. This is the most common SQL command by far.
WHERE age > 18 keeps only matching rows. You can combine conditions: WHERE age > 18 AND country = "US". The database scans and filters for you.
ORDER BY score DESC sorts highest first. ASC sorts lowest first. Add LIMIT 10 to get only the top 10. Leaderboards are one query.
INSERT INTO users (name, age) VALUES ("Alice", 14) creates a new row. UPDATE changes existing rows. DELETE removes them. CRUD: Create, Read, Update, Delete.
Put your new knowledge into practice!