Introduction to SQL: SELECT Queries
What is SQL?
SQL (Structured Query Language) is how you talk to relational databases — asking for data, filtering it, and combining it across tables.
The Basic SELECT
SELECT name, email FROM users; This returns the name and email columns for every row in the users table. Use * to select all columns, though naming columns explicitly is better practice.
Filtering with WHERE
SELECT * FROM users WHERE age >= 18 AND country = 'India'; Sorting and Limiting
SELECT title, views FROM tutorials ORDER BY views DESC LIMIT 10; Joining Tables
Real data usually lives across multiple related tables. A JOIN combines rows from two tables based on a matching column:
SELECT orders.id, users.name FROM orders JOIN users ON orders.user_id = users.id; Grouping and Aggregating
SELECT category, COUNT(*) AS total FROM tutorials GROUP BY category ORDER BY total DESC; Common aggregate functions: COUNT(), SUM(), AVG(), MIN(), MAX().
A Common Beginner Mistake
Forgetting that WHERE filters rows before grouping, while HAVING filters groups after aggregation:
SELECT category, COUNT(*) AS total FROM tutorials GROUP BY category HAVING COUNT(*) > 5; Practice Exercise
Given a students table and an enrollments table, write a query that lists each student’s name alongside how many courses they’re enrolled in, sorted highest first.
Leave a Reply