|
Getting your Trinity Audio player ready...
|
If SQL were a car, SELECT would be the steering wheel.
Without it, you can’t explore data, analyze reports, or build dashboards.
As a Data Analyst or MIS professional, 70% of your work will be writing SELECT queries.
What is SELECT in SQL?
The SELECT statement is used to retrieve data from a database table.
Basic Syntax (Same in PostgreSQL & MySQL)
SELECT column1, column2
FROM table_name;If you want all columns:
SELECT * FROM table_name;FMCG Dataset Tables We’ll Use
- employees
- customers
- products
- sales
- inventory
Select All Data
SELECT * FROM customers;Returns every column and every row.
⚠ Not recommended in production — better to select only required columns.
Select Specific Columns
SELECT customer_id, customer_name, city
FROM customers;✔ Cleaner
✔ Faster
✔ Professional
Filter Data Using WHERE
This is where real analysis starts.
SELECT *
FROM sales
WHERE sale_amount > 5000;Example: Sales from Raipur
SELECT *
FROM customers
WHERE city = 'Raipur';Works same in PostgreSQL and MySQL.
Read More: UPDATE Data in SQL
Using AND / OR Conditions
SELECT *
FROM products
WHERE category = 'Beverages'
AND price > 50;Sorting Data (ORDER BY)
SELECT *
FROM sales
ORDER BY sale_amount DESC;Ascending:
ORDER BY sale_amount ASC;Limit Number of Rows
Very useful when checking large datasets.
PostgreSQL
SELECT *
FROM sales
LIMIT 10;MySQL
SELECT *
FROM sales
LIMIT 10;Same syntax 👍
Select Distinct Values
Get unique cities:
SELECT DISTINCT city
FROM customers;Very common in reporting.
Using Aliases (AS)
Makes reports readable.
SELECT
customer_name AS "Customer Name",
city AS "Customer City"
FROM customers;PostgreSQL supports double quotes for alias with spaces.
MySQL also supports it (with quotes depending on mode).
Using Functions in SELECT
Count total customers
SELECT COUNT(*)
FROM customers;Average sale amount
SELECT AVG(sale_amount)
FROM sales;Pattern Matching (LIKE)
Customers starting with ‘A’
SELECT *
FROM customers
WHERE customer_name LIKE 'A%';Works same in both databases.
BETWEEN Condition
SELECT *
FROM sales
WHERE sale_amount BETWEEN 1000 AND 5000;Very common in financial reporting.
IN Condition
SELECT *
FROM customers
WHERE city IN ('Raipur', 'Bilaspur', 'Durg');Cleaner than multiple OR conditions.
NULL Handling
SELECT *
FROM sales
WHERE discount IS NULL;⚠ Important: use IS NULL, not = NULL.
Real FMCG Analysis Example
Top 5 Highest Sales
SELECT *
FROM sales
ORDER BY sale_amount DESC
LIMIT 5;Total Sales Per City (Preview for GROUP BY topic)
SELECT city, SUM(sale_amount)
FROM sales
GROUP BY city;PostgreSQL vs MySQL Differences in SELECT
| Feature | PostgreSQL | MySQL |
|---|---|---|
| LIMIT | ✅ Yes | ✅ Yes |
| Case sensitivity | Case-sensitive by default | Case-insensitive (usually) |
| ILIKE (case-insensitive search) | ✅ Supported | ❌ Not supported |
Case-Insensitive Search
PostgreSQL:
SELECT *
FROM customers
WHERE customer_name ILIKE 'a%';MySQL:
SELECT *
FROM customers
WHERE LOWER(customer_name) LIKE 'a%';Why SELECT is Critical for Data Analysts
You will use SELECT to:
- Build dashboards
- Create reports
- Extract data for Excel
- Analyze trends
- Identify duplicates
- Filter incorrect records
- Create summary tables
Without mastering SELECT, you cannot move to JOIN, GROUP BY, or advanced analytics.
Best Practices
✔ Avoid SELECT * in real systems
✔ Always filter data properly
✔ Use LIMIT when exploring large tables
✔ Use aliases for readable output
✔ Combine SELECT with WHERE + ORDER BY
Summary
The SELECT statement is used to retrieve data from tables in PostgreSQL and MySQL. It allows filtering with WHERE, sorting using ORDER BY, limiting results, removing duplicates with DISTINCT, and applying conditions like BETWEEN and IN. SELECT is the foundation of data analysis and reporting. Mastering it is essential before learning joins, grouping, and advanced SQL concepts.