SQL SELECT Statement
1. What is SELECT in SQL?
● SELECT is used to retrieve data from database tables.
● The result is stored in a result-set (virtual table).
2. Basic SELECT Syntax
SELECT column1, column2, ... FROM table_name;
To get all columns:
e
SELECT * FROM table_name;
ag
dh
Example:
SELECT id, email FROM Employee2;
ay
sh
ak
SELECT * FROM customers;
e
ag
dh
ay
3. SELECT with WHERE Clause
sh
● Filters data based on specific conditions.
ak
SELECT * FROM table_name WHERE condition;
Example:
SELECT * FROM customer WHERE customer_id = 'IM-15070';
SELECT * FROM customers WHERE customer_name = 'Irene Maddox';
e
ag
dh
ay
sh
ak
4. SELECT DISTINCT
● Removes duplicate values from the result.
SELECT DISTINCT column_name FROM table_name;
Example:
SELECT DISTINCT state FROM customer;
Output:
e
5. SELECT COUNT()
ag
● Returns the number of rows (records).
dh
ay
1. SELECT COUNT(column_name) FROM table_name;
sh
Example:
ak
SELECT COUNT(state) FROM customer;
2. SELECT COUNT(DISTINCT column_name) FROM table_name WHERE condition;
Example:
SELECT COUNT(DISTINCT state) FROM customer;
e
ag
dh
ay
sh
6. SELECT with IN
ak
Used to filter data based on a list of multiple values.
SELECT column1, column2
FROM table_name
WHERE column_name IN (value1, value2, ...);
Example 1 :
SELECT * FROM Products WHERE Category IN ('Furniture', 'Technology');
e
ag
Fetches all products that belong to either the Furniture or Technology category.
dh
Example 2 :
SELECT * FROM Customer WHERE Region IN ('West', 'East');
ay
sh
ak
Retrieves all customers located in the West or East regions.
7. SELECT with ORDER BY
Used to sort the result set by one or more columns.
Example:
SELECT product_id , Sales FROM Sales ORDER BY Sales DESC;
e
ag
dh
ay
sh
ak
Summary Table:
Feature Example SQL Command
All columns SELECT * FROM table_name;
Specific columns SELECT name, age FROM table_name;
Filtered data SELECT * FROM table_name WHERE age > 25;
Unique values SELECT DISTINCT city FROM table_name;
Count total rows SELECT COUNT(*) FROM table_name;
Count unique values SELECT COUNT(DISTINCT salary) FROM table_name;
Match multiple values SELECT * FROM table_name WHERE name IN ('A', 'B');
e
ag
dh
ay
sh
ak
SQL SELECT – Practice Questions
1. Basic SELECT
1. Show all columns from the Products table.
2. Retrieve only Product_ID, Product_Name, and Category from the Products
table.
3. Display all records from the Customers table.
2. SELECT with WHERE
4. Find all orders placed by customer ID 'CA-2017-152156'.
5. Show all customers from the 'East' region.
e
ag
6. Retrieve products that belong to the category 'Technology'.
dh
7. Show all products with sales greater than 500.
ay
3. SELECT DISTINCT
sh
8. List all unique product categories.
ak
9. Show distinct customer segments.
10.Display all distinct regions from the Customers table.
4. SELECT COUNT()
11.Count the total number of orders.
12.Count how many unique customers are in the database.
13.Count how many products belong to the category 'Furniture'.
14.Count the number of orders placed in the 'West' region.
5. SELECT with IN
15.Show all products in the 'Furniture' or 'Office Supplies' categories.
16.Retrieve all customers located in 'South' or 'Central' regions.
17.Display orders with IDs in the list: 'CA-2015-100111', 'CA-2016-123456',
'CA-2017-654321'.
6. SELECT with ORDER BY
18.List all customers sorted by Customer_Name in ascending order.
19.Display all products sorted by Sales in descending order.
20.Show orders sorted by Order_Date from oldest to newest.
e
ag
dh
ay
sh
ak
You can download the complete set of SQL notes and practice files from this GitHub
repository:
👉 SQL-resources-and-tutorials by akshay-dhage