The Ultimate Cheat Sheet for SQL SELECT Queries

savitry.in
0

The SELECT statement is the backbone of SQL, allowing you to retrieve and manipulate data from relational databases. This guide covers the essential components and advanced features of SELECT queries to help you master data retrieval in SQL.


1. Basic Syntax

SELECT column1, column2, ...
FROM table_name;
  • Retrieve specific columns from a table.
SELECT *
FROM table_name;
  • Retrieve all columns from a table.

2. Filtering Data (WHERE Clause)

SELECT column1, column2
FROM table_name
WHERE condition;
  • Use operators like =, >, <, >=, <=, <> (not equal).

Examples:

SELECT *
FROM employees
WHERE age > 30;
SELECT *
FROM products
WHERE price BETWEEN 10 AND 50;
SELECT *
FROM users
WHERE name LIKE 'A%'; -- Starts with 'A'

3. Sorting Data (ORDER BY)

SELECT column1, column2
FROM table_name
ORDER BY column1 ASC | DESC;
  • ASC (default) for ascending order, DESC for descending.

4. Limiting Rows

SELECT column1, column2
FROM table_name
LIMIT number_of_rows;

5. Aggregations (GROUP BY and HAVING)

  • GROUP BY: Group rows with similar values.
  • HAVING: Filter groups based on conditions.

Example:

SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;

6. Joins

Inner Join:

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

Left Join:

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

Right Join:

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

Full Outer Join:

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

7. Subqueries

  • Query within a query.

Example:

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

8. Common Functions

String Functions:

  • CONCAT(str1, str2)
  • LOWER(), UPPER()
  • LENGTH()

Math Functions:

  • SUM(), AVG(), MIN(), MAX()
  • ROUND()

Date Functions:

  • NOW(), CURDATE()
  • DATEDIFF(date1, date2)

9. Aliases

  • Rename columns or tables for better readability.
SELECT column_name AS alias_name
FROM table_name;

10. Distinct Values

  • Eliminate duplicate rows.
SELECT DISTINCT column1
FROM table_name;

11. Combining Results (UNION)

  • Combine results from multiple SELECT queries.
SELECT column1 FROM table1
UNION
SELECT column1 FROM table2;

12. Advanced Filtering

IN Clause:

SELECT *
FROM table_name
WHERE column_name IN (value1, value2, ...);

NOT IN Clause:

SELECT *
FROM table_name
WHERE column_name NOT IN (value1, value2, ...);

Final Thoughts

This cheat sheet provides a quick reference to the most commonly used SELECT query features in SQL. Whether you're a beginner or a seasoned developer, these tips will help you retrieve data efficiently and accurately. Experiment with these techniques to become a SQL pro!" }

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 !