What is SQL? How to Query Databases
SQL • Concepts Explained • 7 min read
SQL is how you talk to databases. Learn SELECT, INSERT, UPDATE, JOIN, and other essential queries.
What is SQL? How to Query Databases
SQL (Structured Query Language) is how you talk to databases. Every web application stores data somewhere, and SQL is how you retrieve and manipulate it.
What is SQL?
SQL is a language for managing data in relational databases. You use it to create tables, insert data, query information, and update records.
Basic SQL Queries
-- Create a table
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
email VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Insert data
INSERT INTO users (name, email) VALUES ('John', 'john@example.com');
-- Query data
SELECT * FROM users;
SELECT name, email FROM users WHERE id = 1;
-- Update data
UPDATE users SET name = 'Jane' WHERE id = 1;
-- Delete data
DELETE FROM users WHERE id = 1;
Filtering with WHERE
-- Comparison operators
SELECT * FROM users WHERE age > 18;
SELECT * FROM users WHERE name LIKE 'J%';
SELECT * FROM users WHERE city IN ('Lagos', 'Abuja');
-- Multiple conditions
SELECT * FROM users WHERE age > 18 AND city = 'Lagos';
Sorting and Limiting
-- Sort results
SELECT * FROM users ORDER BY name ASC;
SELECT * FROM users ORDER BY created_at DESC;
-- Limit results
SELECT * FROM users LIMIT 10;
SELECT * FROM users LIMIT 10 OFFSET 20; -- Pagination
Joining Tables
-- Get users with their orders
SELECT users.name, orders.total
FROM users
INNER JOIN orders ON users.id = orders.user_id;
-- Left join (include users without orders)
SELECT users.name, orders.total
FROM users
LEFT JOIN orders ON users.id = orders.user_id;
Aggregate Functions
-- Count records
SELECT COUNT(*) FROM users;
-- Sum and average
SELECT SUM(total), AVG(total) FROM orders;
-- Group by
SELECT city, COUNT(*) FROM users GROUP BY city;
Where to Learn SQL
- SQLBolt — Interactive tutorials
- W3Schools — Reference and examples
- Mode Analytics — Real data practice
Note: SQL is one of the most useful skills for any developer. Even a basic understanding of SQL makes you more valuable in the job market.