Structured Query Language (SQL) is the backbone of data analysis. Whether you’re exploring sales trends, generating business reports, or building dashboards, SQL helps you query, transform, and understand data efficiently.
For aspiring and professional data analysts, mastering SQL is non-negotiable. In this guide, we’ll walk through 10 essential SQL commands with practical explanations.
Core SQL Commands
Before diving into examples, here’s a quick reference of the key SQL commands you’ll use frequently:
- SELECT – Retrieve data from tables
- WHERE – Filter rows based on conditions
- GROUP BY – Aggregate and group data
- HAVING – Filter grouped results with aggregates
- JOIN – Combine data from multiple tables
- ORDER BY – Sort results ascending/descending
- LIMIT – Restrict number of rows returned
- CASE – Implement if/else logic in queries
- DISTINCT – Remove duplicate rows
- UNION – Merge results from multiple queries
Core SQL Commands
1. SELECT – Retrieve Data
SELECT first_name, last_name, salary
FROM employees
WHERE department = 'Sales';
Description: Retrieves the first name, last name, and salary of all employees who work in the Sales department.
Use case: Quickly get specific columns from a table based on a condition.
2. WHERE – Filter Rows
SELECT product_name, unit_price
FROM products
WHERE category_id = 1 AND unit_price > 50;
Description:Fetches product names and prices from category 1 where the unit price is greater than 50.
Use case:Narrow down results based on one or more conditions.
3. GROUP BY – Summarize Data
SELECT department, COUNT(*) AS employee_count, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
Description: Groups employees by department and calculates the number of employees and the average salary in each department.
Use case: Analyze aggregated data per group.
4. HAVING – Filter Groups
SELECT category, COUNT(*) AS product_count
FROM products
GROUP BY category
HAVING COUNT(*) > 10;
Description: Groups products by category and only shows categories that have more than 10 products.
Use case: Apply conditions on aggregated results after grouping.
5. JOIN – Combine Tables
SELECT o.order_id, c.customer_name, o.order_date
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id;
Description: Retrieves order IDs, customer names, and order dates by combining orders with their corresponding customers.
Use case: Merge data from multiple related tables in a single query.
6. ORDER BY – Sort Results
SELECT product_name, unit_price
FROM products
ORDER BY unit_price DESC, product_name ASC;
Description: Lists products sorted by price in descending order, and for products with the same price, sorts alphabetically by name.
Use case: Display data in a specific order to identify trends or rankings.
7. LIMIT – Restrict Rows
SELECT product_name, units_sold
FROM sales
ORDER BY units_sold DESC
LIMIT 5;
Description: Retrieves the top 5 best-selling products based on units sold.
Use case: Focus on a subset of data, such as top performers.
8. CASE – Conditional Logic
SELECT product_name,
CASE
WHEN unit_price < 20 THEN 'Budget'
WHEN unit_price < 50 THEN 'Mid-range'
ELSE 'Premium'
END AS price_category
FROM products;
Description: Categorizes products into “Budget”, “Mid-range”, or “Premium” based on their unit price.
Use case: Create dynamic categories or labels directly in SQL.
9. DISTINCT – Remove Duplicates
SELECT DISTINCT category, supplier_id
FROM products
ORDER BY category;
Description: Returns unique combinations of category and supplier, removing any duplicate entries.
Use case: Eliminate duplicate data to ensure accuracy in reporting.
10. UNION – Merge Queries
SELECT product_id, product_name, 'Active' AS status
FROM current_products
UNION
SELECT product_id, product_name, 'Discontinued'
FROM discontinued_products;
Description: Combines active and discontinued products into a single list, marking their status accordingly
Use case: Consolidate data from multiple sources into one result set.
Conclusion
Mastering these 10 SQL commands is the foundation for any data analyst or developer. They allow you to retrieve, filter, aggregate, sort, and combine data effectively. By practicing these commands, you’ll be able to handle real-world datasets and make informed business decisions faster