The Ultimate Cheat Sheet for SQL SELECT Queries

Admin, Student's Library
0

The Complete SQL SELECT Statement Guide

SQL SELECT statement visualization

Key Topics: Basic Syntax | Filtering & Sorting | Joins & Subqueries | Aggregation Functions

The SELECT statement is the backbone of SQL, allowing you to retrieve and manipulate data from relational databases. This comprehensive guide covers everything from basic syntax to advanced query techniques, complete with practical examples to help you master data retrieval in SQL.

1. Basic SELECT Syntax

The fundamental structure for retrieving data:

Retrieve specific columns:

SELECT column1, column2, ...
FROM table_name;

Retrieve all columns:

SELECT *
FROM table_name;

2. Filtering Data (WHERE Clause)

Filter records using conditions:

SELECT column1, column2
FROM table_name
WHERE condition;

Common Operators:
= (Equal), > (Greater than), < (Less than),
>= (Greater than or equal), <= (Less than or equal),
<> (Not equal), LIKE (Pattern matching)

Practical Examples:

-- Employees over 30 years old
SELECT *
FROM employees
WHERE age > 30;

-- Products priced between $10 and $50
SELECT *
FROM products
WHERE price BETWEEN 10 AND 50;

-- Users with names starting with 'A'
SELECT *
FROM users
WHERE name LIKE 'A%';

3. Sorting Results (ORDER BY)

SELECT column1, column2
FROM table_name
ORDER BY column1 [ASC|DESC];

ASC for ascending (default), DESC for descending order.

4. Limiting Results

SELECT column1, column2
FROM table_name
LIMIT number_of_rows;

5. Aggregations (GROUP BY and HAVING)

SELECT department, COUNT(*) as employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;
  • GROUP BY: Groups rows with similar values
  • HAVING: Filters groups (like WHERE for GROUP BY)

6. Joining Tables

INNER JOIN (matches only):

SELECT a.column1, b.column2
FROM table1 a
JOIN table2 b ON a.common_column = b.common_column;

LEFT JOIN (all from left table):

SELECT a.column1, b.column2
FROM table1 a
LEFT JOIN table2 b ON a.common_column = b.common_column;

RIGHT JOIN (all from right table):

SELECT a.column1, b.column2
FROM table1 a
RIGHT JOIN table2 b ON a.common_column = b.common_column;

FULL OUTER JOIN (all records):

SELECT a.column1, b.column2
FROM table1 a
FULL OUTER JOIN table2 b ON a.common_column = b.common_column;

7. Subqueries

Queries within queries for complex operations:

SELECT name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

8. Essential SQL Functions

Category Functions Example
String CONCAT(), LOWER(), UPPER(), LENGTH() SELECT CONCAT(first_name, ' ', last_name)
Math SUM(), AVG(), MIN(), MAX(), ROUND() SELECT AVG(price) FROM products
Date NOW(), CURDATE(), DATEDIFF() SELECT DATEDIFF(end_date, start_date)

9. Aliases

Improve readability with column/table aliases:

SELECT column_name AS alias_name
FROM table_name;

-- Table alias example:
SELECT e.name, d.department_name
FROM employees AS e
JOIN departments AS d ON e.dept_id = d.id;

10. DISTINCT Values

Remove duplicate rows from results:

SELECT DISTINCT column1
FROM table_name;

11. Combining Results (UNION)

SELECT column1 FROM table1
UNION
SELECT column1 FROM table2;

Note: UNION removes duplicates. Use UNION ALL to keep duplicates.

12. Advanced Filtering

IN Clause:

SELECT *
FROM products
WHERE category_id IN (1, 5, 7);

NOT IN Clause:

SELECT *
FROM employees
WHERE department_id NOT IN (3, 5);

Final Tips:

  • Always test queries with LIMIT before running on large tables
  • Use EXPLAIN to analyze query performance
  • Proper indexing dramatically improves SELECT performance
  • Format your SQL for readability

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !