0% found this document useful (0 votes)
2 views4 pages

Module 4 SQL

This document provides a comprehensive guide to SQL SELECT commands, including syntax for selecting, filtering, grouping, aggregating, sorting, and limiting data. It includes sample commands for each section and offers a downloadable cheat sheet PDF as well as practice questions to reinforce learning. The practice questions cover various topics such as SELECT essentials, filtering data, grouping and aggregating, and sorting and limiting.

Uploaded by

gatherdevelopers
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Module 4 SQL

This document provides a comprehensive guide to SQL SELECT commands, including syntax for selecting, filtering, grouping, aggregating, sorting, and limiting data. It includes sample commands for each section and offers a downloadable cheat sheet PDF as well as practice questions to reinforce learning. The practice questions cover various topics such as SELECT essentials, filtering data, grouping and aggregating, and sorting and limiting.

Uploaded by

gatherdevelopers
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

SQL SELECT Essentials


Syntax:

sql
CopyEdit
SELECT column1, column2 FROM table_name;

✅ Sample Commands:
sql
CopyEdit
-- Select specific columns
SELECT first_name, last_name FROM employees;

-- Select all columns


SELECT * FROM customers;

-- Select literal values and expressions


SELECT 'Hello World', 2 + 3;

-- Rename columns using aliases


SELECT first_name AS "First Name", salary * 1.1 AS "Updated Salary" FROM
employees;

🟩 2. Filtering Data (WHERE Clause)


Syntax:

sql
CopyEdit
SELECT column1 FROM table_name WHERE condition;

✅ Sample Commands:
sql
CopyEdit
-- Filter using equality
SELECT * FROM orders WHERE status = 'Shipped';

-- Filter with multiple conditions


SELECT * FROM employees WHERE department = 'HR' AND salary > 40000;

-- Use IN to filter by multiple values


SELECT * FROM products WHERE category IN ('Electronics', 'Books');

-- Use BETWEEN for range filtering


SELECT * FROM employees WHERE hire_date BETWEEN '2020-01-01' AND '2021-12-31';

-- Use LIKE for pattern matching


SELECT * FROM customers WHERE email LIKE '%@[Link]';

-- Check for NULL values


SELECT * FROM orders WHERE shipped_date IS NULL;

🟨 3. Grouping and Aggregating Data


Syntax:

sql
CopyEdit
SELECT column, AGG_FUNC(column) FROM table_name GROUP BY column;

✅ Sample Commands:
sql
CopyEdit
-- Average salary by department
SELECT department, AVG(salary) FROM employees GROUP BY department;

-- Count of orders per customer


SELECT customer_id, COUNT(*) AS order_count FROM orders GROUP BY customer_id;

-- Total revenue per product


SELECT product_id, SUM(price * quantity) AS total_revenue FROM order_items
GROUP BY product_id;

-- Minimum and maximum salary per department


SELECT department, MIN(salary), MAX(salary) FROM employees GROUP BY
department;

-- Filtering groups with HAVING


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

🟧 4. Sorting and Limiting Data


Syntax (Sorting):

sql
CopyEdit
SELECT * FROM table_name ORDER BY column [ASC|DESC];

Syntax (Limiting):

 MySQL/PostgreSQL:
sql
CopyEdit
SELECT * FROM table_name LIMIT number OFFSET number;

 SQL Server:

sql
CopyEdit
SELECT TOP number * FROM table_name;

✅ Sample Commands:
sql
CopyEdit
-- Sort employees by salary in descending order
SELECT * FROM employees ORDER BY salary DESC;

-- Sort products by name alphabetically


SELECT * FROM products ORDER BY product_name ASC;

-- Get top 5 highest-paid employees


SELECT * FROM employees ORDER BY salary DESC LIMIT 5;

-- Skip first 10 products and show next 5


SELECT * FROM products ORDER BY price LIMIT 5 OFFSET 10;

-- SQL Server version of top 3 recent orders


SELECT TOP 3 * FROM orders ORDER BY order_date DESC;

Part 1: Cheat Sheet PDF – “SQL SELECT Quick Guide”

I’ve compiled a clean and structured SQL SELECT Cheat Sheet with:

 Core syntax
 Key concepts
 Multiple sample queries for each section:
o SELECT essentials
o Filtering with WHERE
o Grouping & aggregation
o Sorting and limiting

📄 Download Your Cheat Sheet PDF: 👉 Click here to download SQL SELECT Cheat Sheet
(PDF)

✅ Part 2: Practice Questions: SQL SELECT Mastery


Here are 15 practice questions covering all the topics:

🔹 Section 1: SELECT Essentials

1. Write a query to select all columns from a students table.


2. Select only the first_name and last_name from an employees table.
3. Select all columns from the products table and rename price as unit_price.

🔹 Section 2: Filtering Data

4. Get all employees who work in the "Marketing" department.


5. Show customers whose email ends with @[Link].
6. List all orders placed between '2023-01-01' and '2023-06-30'.
7. Retrieve employees who earn more than 60000 and belong to the "HR" department.
8. Get all items where stock_quantity is less than 10 or category is 'Clearance'.
9. Find all customers with no phone number listed (phone IS NULL).

🔹 Section 3: Grouping and Aggregating

10. Show the average salary of employees for each department.


11. Count the number of orders placed by each customer.
12. Find the departments that have more than 15 employees.
13. Get the maximum and minimum prices for each product category.

🔹 Section 4: Sorting and Limiting

14. List the top 5 products with the highest price.


15. Show the 10 most recently added customers sorted by created_at date (latest first).

You might also like