// Speak the language of data.
DATA IS THE BACKBONE OF EVERY APPLICATION.
SQL (Structured Query Language) is the standard language for relational databases. From simple blogs to massive enterprise systems, every application needs to store and retrieve data.
WHY SQL?
SQL powers virtually every web application. MySQL, PostgreSQL, Oracle, SQL Server—all use SQL. Learn once, work with any database.
Click a lesson to begin
What is SQL? Databases, tables, and your first query.
BeginnerWHERE, ORDER BY, and filtering data.
BeginnerCREATE TABLE, data types, and schema design.
BeginnerModifying data in your database.
BeginnerINNER JOIN, LEFT JOIN, and combining tables.
IntermediateGROUP BY, COUNT, SUM, AVG, and HAVING.
IntermediateNested queries for complex data retrieval.
IntermediateCREATE INDEX and query optimization.
IntermediateNormalization and schema design principles.
AdvancedPRIMARY KEY, FOREIGN KEY, and validation.
AdvancedACID properties, COMMIT, and ROLLBACK.
AdvancedStored procedures, triggers, and automation.
AdvancedSQL has been the standard for relational databases since the 1970s. It powers everything from simple blogs to massive platforms like Facebook, Twitter, and Instagram.
Unlike NoSQL databases, relational databases offer strong consistency, ACID compliance, and powerful query capabilities. They're ideal for applications where data integrity is critical.
Major database systems—MySQL, PostgreSQL, Oracle, SQL Server—all support SQL. Learn once, work with any of them.
Data is king. SQL is how you speak to it.
SQL (Structured Query Language) is the standard language for managing and manipulating relational databases. It allows you to create, read, update, and delete data, as well as manage database structure.
-- Select all columns from a table SELECT * FROM users; -- Select specific columns SELECT name, email FROM users;
1. What does SQL stand for?
SELECT is used to retrieve data from a database. You can filter, sort, and limit results.
-- Filter by condition SELECT * FROM users WHERE age >= 18; -- Multiple conditions SELECT * FROM users WHERE country = 'USA' AND age > 21; -- OR condition SELECT * FROM products WHERE price < 10 OR price > 100;
-- Sort ascending (default) SELECT * FROM users ORDER BY name; -- Sort descending SELECT * FROM users ORDER BY created_at DESC; -- Sort by multiple columns SELECT * FROM users ORDER BY country ASC, name ASC;
-- Get top 10 results SELECT * FROM users ORDER BY score DESC LIMIT 10;
1. How do you sort results in descending order?
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
age INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- Add a column ALTER TABLE users ADD COLUMN phone VARCHAR(20); -- Drop a column ALTER TABLE users DROP COLUMN phone; -- Modify a column ALTER TABLE users MODIFY COLUMN age INT NOT NULL;
1. Which data type would you use for a phone number?
-- Insert a single row
INSERT INTO users (name, email, age)
VALUES ('Alice', 'alice@example.com', 25);
-- Insert multiple rows
INSERT INTO users (name, email) VALUES
('Bob', 'bob@example.com'),
('Charlie', 'charlie@example.com');
-- Update with condition UPDATE users SET age = 26 WHERE name = 'Alice'; -- Update multiple columns UPDATE users SET age = 27, city = 'NYC' WHERE id = 1;
-- Delete with condition DELETE FROM users WHERE id = 5; -- Delete all rows (use with caution!) DELETE FROM users;
1. Which command removes rows from a table?
Joins combine data from multiple tables based on related columns.
SELECT users.name, orders.total FROM users INNER JOIN orders ON users.id = orders.user_id;
-- Includes all users, even without orders SELECT users.name, orders.total FROM users LEFT JOIN orders ON users.id = orders.user_id;
SELECT users.name, products.name AS product, orders.quantity FROM users JOIN orders ON users.id = orders.user_id JOIN products ON orders.product_id = products.id;
1. Which join returns all rows from the left table?
-- Count all rows SELECT COUNT(*) FROM users; -- Count non-null values SELECT COUNT(email) FROM users;
-- Count users per country SELECT country, COUNT(*) as user_count FROM users GROUP BY country;
-- Filter after grouping SELECT country, COUNT(*) as user_count FROM users GROUP BY country HAVING user_count > 10;
1. Which function calculates the average value?
A subquery is a query nested inside another query. They allow you to use results from one query in another.
-- Find users who ordered more than average
SELECT name FROM users
WHERE id IN (SELECT user_id FROM orders
GROUP BY user_id
HAVING SUM(total) > (SELECT AVG(order_total) FROM (SELECT SUM(total) as order_total FROM orders GROUP BY user_id) AS avg_order));
-- Use subquery as a table
SELECT country, avg_age FROM (
SELECT country, AVG(age) as avg_age
FROM users
GROUP BY country
) AS country_stats
WHERE avg_age > 30;
-- Find each user's latest order
SELECT * FROM orders o1
WHERE order_date = (
SELECT MAX(order_date)
FROM orders o2
WHERE o1.user_id = o2.user_id
);
1. What is a subquery?
Indexes are data structures that improve query speed. Think of them like a book index - they help you find information faster.
-- Single column index CREATE INDEX idx_users_email ON users(email); -- Composite index CREATE INDEX idx_orders_user_date ON orders(user_id, order_date); -- Unique index CREATE UNIQUE INDEX idx_users_phone ON users(phone);
-- See how a query executes EXPLAIN SELECT * FROM users WHERE email = 'alice@example.com';
1. Which command creates an index on the email column?
Normalization organizes data to reduce redundancy and improve integrity.
-- Bad: Repeating data orders(order_id, customer_name, customer_email, product, price) -- Good: Normalized customers(customer_id, name, email) products(product_id, name, price) orders(order_id, customer_id, product_id)
1. What is the goal of database normalization?
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE orders (
id INT PRIMARY KEY,
user_id INT,
total DECIMAL(10,2),
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE products (
id INT PRIMARY KEY,
price DECIMAL(10,2) CHECK (price > 0),
quantity INT CHECK (quantity >= 0)
);
1. Which constraint ensures unique values?
-- Start transaction BEGIN TRANSACTION; -- Make changes UPDATE accounts SET balance = balance - 100 WHERE user_id = 1; UPDATE accounts SET balance = balance + 100 WHERE user_id = 2; -- Save changes COMMIT; -- Or undo changes ROLLBACK;
BEGIN; UPDATE users SET name = 'Alice' WHERE id = 1; SAVEPOINT sp1; UPDATE users SET name = 'Bob' WHERE id = 2; ROLLBACK TO SAVEPOINT sp1; COMMIT;
1. Which command undoes all changes in a transaction?
A virtual table based on a query. Views simplify complex queries and restrict access to data.
-- Create view CREATE VIEW active_users AS SELECT name, email FROM users WHERE active = 1; -- Use view like a table SELECT * FROM active_users; -- Drop view DROP VIEW active_users;
Precompiled SQL code that can be executed as a single unit.
-- Create procedure (MySQL)
DELIMITER //
CREATE PROCEDURE get_user_orders(IN user_id INT)
BEGIN
SELECT * FROM orders WHERE user_id = user_id;
END //
DELIMITER ;
-- Call procedure
CALL get_user_orders(1);
1. What is a view?
You've completed the SQL Mastery guide. You now understand: