Introduction to SQL
Welcome to your first SQL lesson! SQL (Structured Query Language) is the standard language for working with relational databases and is essential for any developer or data professional.
What is SQL?
SQL is used to communicate with databases. It allows you to store, retrieve, update, and delete data, as well as define database structures. Every major database system supports SQL.
Key Features
Basic Queries
-- Select all users
SELECT * FROM users;
-- Select specific columns
SELECT name, email FROM users;
-- Filter results
SELECT * FROM users WHERE age >= 18;
-- Sort results
SELECT * FROM products ORDER BY price DESC;
Filtering and Conditions
-- Multiple conditions
SELECT * FROM users
WHERE age >= 18 AND country = 'USA';
-- Pattern matching
SELECT * FROM products
WHERE name LIKE '%phone%';
-- Check for values in a list
SELECT * FROM orders
WHERE status IN ('pending', 'processing');
Aggregations
-- Count records
SELECT COUNT(*) FROM users;
-- Group and aggregate
SELECT country, COUNT(*) as user_count
FROM users
GROUP BY country
ORDER BY user_count DESC;
-- Calculate averages
SELECT AVG(price) as avg_price
FROM products
WHERE category = 'Electronics';
Joins
-- Join tables together
SELECT orders.id, users.name, orders.total
FROM orders
JOIN users ON orders.user_id = users.id
WHERE orders.status = 'completed';
Why Learn SQL?
- Universal - Works with PostgreSQL, MySQL, SQLite, and more
- Essential skill - Required for backend, data, and DevOps roles
- Data analysis - Query and analyze data efficiently
- Career growth - High demand across all industries
Getting Started
In the next lesson, we'll set up a database and write our first queries.