SELECT Basics — Filtering, Sorting, DISTINCT
SELECT — filter, sort, DISTINCT
1. The Most-Used Statement in Software
If you measure SQL usage by row count, no statement comes close to SELECT. Every product list,
every search box, every report, every dashboard begins as a SELECT. By the end of this module
you will know how to choose the rows you want (WHERE), order them
(ORDER BY), eliminate duplicates (DISTINCT), paginate them
(LIMIT/OFFSET), and handle the most subtle topic in SQL — NULL.
SELECT। প্রতিটি product তালিকা, search box, report, dashboard — সব SELECT দিয়েই শুরু হয়। এই module শেষে আপনি জানবেন — row বাছাই (WHERE), sort (ORDER BY), duplicate বাদ (DISTINCT), pagination (LIMIT/OFFSET) এবং SQL-এর সবচেয়ে সূক্ষ্ম বিষয় — NULL — কীভাবে কাজ করে।
SELECT … FROM … WHERE … ORDER BY …, the database
evaluates it roughly in this order: FROM → WHERE → SELECT →
DISTINCT → ORDER BY → LIMIT. Knowing this saves you from many
"but why doesn't it work?" moments.
যদিও আমরা
SELECT … FROM … WHERE … ORDER BY … এই ক্রমে লিখি, database প্রকৃতপক্ষে এটি চালায় FROM → WHERE → SELECT → DISTINCT → ORDER BY → LIMIT ক্রমে। এই ক্রম মনে থাকলে অনেক বিভ্রান্তি কমে যাবে।
2. SELECT … FROM — Picking Columns
The simplest SELECT picks a list of columns from a single table. The literal
* means "all columns" — convenient in interactive sessions but discouraged in production
code, because adding a new column later silently changes the result shape and can break consumers.
SELECT মানে একটি table থেকে কয়েকটি column বের করে আনা। * চিহ্ন মানে "সব column" — interactive কাজের জন্য সুবিধাজনক হলেও production কোডে এড়িয়ে যাওয়া ভালো; নতুন column যুক্ত হলে আগের code চুপচাপ ভেঙে যায়।
-- Pick just the columns you need (good practice)
SELECT name, dept, cgpa
FROM students;
2.1 — Aliases with AS
You can rename columns in the result using AS. This is essential for human-friendly reports
and for distinguishing computed columns:
SELECT
title AS item,
price AS price_bdt,
price * 1.05 AS price_with_vat
FROM products;
3. WHERE — Filtering Rows
WHERE applies a predicate — a boolean expression — to each row. Only rows for which
the predicate evaluates to TRUE survive into the result. This is the workhorse of every
real-world query.
WHERE প্রতিটি row-এর উপর একটি boolean expression চালায় — যেগুলোতে এক্সপ্রেশন TRUE হয়, সেগুলো result-এ যায়। বাস্তব জীবনের প্রায় প্রতিটি query-তে WHERE থাকে।
3.1 — Comparison operators
| Operator | Meaning | Example |
|---|---|---|
= | Equal to | dept = 'CSE' |
<> or != | Not equal | status <> 'failed' |
<, <= | Less than (or equal) | cgpa < 3.0 |
>, >= | Greater than (or equal) | price >= 1000 |
BETWEEN | Range (inclusive) | price BETWEEN 100 AND 500 |
IN | Membership | dept IN ('CSE','EEE') |
LIKE | Pattern | name LIKE 'A%' |
IS NULL | Tests for NULL | cgpa IS NULL |
-- Orders worth more than 500 BDT, that have already shipped.
SELECT customer, amount, status
FROM orders
WHERE amount > 500
AND status = 'shipped';
4. ORDER BY — Sorting the Result
Without ORDER BY, SQL gives no guarantee about the order of rows. If you need them sorted —
and almost every UI does — say so explicitly. ASC means ascending (the default),
DESC means descending. You can sort on multiple columns; the second column breaks ties of
the first, and so on.
ORDER BY না দিলে SQL row-এর কোনো ক্রম গ্যারান্টি দেয় না। UI-এর জন্য সাধারণত sorted ফলাফল দরকার, তাই স্পষ্টভাবে ORDER BY লিখতে হয়। ASC মানে ছোট থেকে বড় (default), DESC মানে বড় থেকে ছোট। একাধিক column দিয়ে sort করা যায় — প্রথম column-এর tie দ্বিতীয় column ভাঙে।
-- Sort by department first, then by cgpa descending
SELECT name, dept, cgpa
FROM students
ORDER BY dept ASC, cgpa DESC;
ORDER BY can refer to a column alias defined in SELECT, or to any expression.
For example: ORDER BY price * 1.05 DESC sorts by price-with-VAT.
ORDER BY alias বা যে কোনো expression ধরে sort করতে পারে — যেমন ORDER BY price * 1.05 DESC।
5. DISTINCT — Eliminating Duplicates
SELECT DISTINCT removes duplicate rows from the result. Duplicate is defined across
all the columns you select — so SELECT DISTINCT dept returns each department once,
but SELECT DISTINCT name, dept only collapses rows where both name and dept match.
SELECT DISTINCT result থেকে duplicate row বাদ দেয়। Duplicate বলতে বোঝায় — আপনি যে যে column SELECT করেছেন সেগুলো একইরকম। তাই SELECT DISTINCT dept প্রতিটি department একবার দেখায়, কিন্তু SELECT DISTINCT name, dept তখনই দুটিকে এক করে যখন name ও dept দুটোই মিলে যায়।
SELECT DISTINCT city
FROM customers
ORDER BY city;
6. LIMIT and OFFSET — Pagination
Most pages of products, posts, and tweets show ~20 rows at a time — never the entire database.
LIMIT n caps the number of rows returned; OFFSET k skips the first
k rows. Together they implement classic page-based pagination.
LIMIT n মানে সর্বোচ্চ nটি row, OFFSET k মানে প্রথম kটি row বাদ দিয়ে শুরু — দুটি মিলে pagination।
| Page (size 5) | Query |
|---|---|
| Page 1 (rows 1–5) | LIMIT 5 OFFSET 0 |
| Page 2 (rows 6–10) | LIMIT 5 OFFSET 5 |
| Page 3 (rows 11–15) | LIMIT 5 OFFSET 10 |
| Page n | LIMIT 5 OFFSET (n-1)*5 |
-- Page 2 with 5 rows per page
SELECT id, title
FROM posts
ORDER BY id
LIMIT 5 OFFSET 5;
ORDER BY the database is free to return rows in any order — page 2 might overlap
page 1 between requests. Always combine LIMIT with a deterministic ORDER BY.
LIMIT-এর সাথে সবসময় ORDER BY দিন। নইলে দুই request-এ একই row বারবার আসতে পারে।
7. NULL — Three-Valued Logic
NULL is SQL's way of saying "the value is unknown / not yet supplied." It is not
zero, not the empty string, not FALSE. Once you accept that, the rest is mechanical:
any comparison with NULL is itself NULL (which is treated as "unknown"). So
price = NULL is never true — even when the price actually is NULL. To test for null you must
use IS NULL or IS NOT NULL.
NULL মানে "value-টি অজানা"। এটি শূন্য নয়, খালি string নয়, FALSE নয়। তাই NULL-এর সাথে যেকোনো তুলনার ফল নিজেই NULL (অজানা)। অর্থাৎ price = NULL কখনোই TRUE নয় — যদি price আসলে NULL-ও হয়। NULL পরীক্ষা করতে হলে IS NULL বা IS NOT NULL ব্যবহার করতে হয়।
-- This returns 0 rows — NULL = NULL is NULL, not TRUE!
SELECT name FROM students WHERE cgpa = NULL;
-- The correct way:
SELECT name FROM students WHERE cgpa IS NULL;
✅ Right way (সঠিক)
cgpa IS NULLcgpa IS NOT NULLCOALESCE(cgpa, 0) > 3.0
⚠️ Wrong way (ভুল)
cgpa = NULLcgpa <> NULLcgpa = ''— empty string ≠ NULL
8. Putting It All Together
A real-world query usually combines all of the above. Here is the kind of statement you will write inside a Daraz product-listing page handler:
-- "Show me the 3 cheapest in-stock electronics."
SELECT DISTINCT title, price, stock
FROM products
WHERE category = 'electronics'
AND stock > 0
ORDER BY price ASC
LIMIT 3;
ক্রম মনে রাখুন — FROM → WHERE → SELECT → DISTINCT → ORDER BY → LIMIT।
9. Practice Problems
-
List every student's name and department.প্রতিটি student-এর নাম ও department দেখান।
✨ Show Answer
ans1.sqlSELECT name, dept FROM students; -
Find all CSE students.CSE department-এর সব student দেখান।
✨ Show Answer
ans2.sqlSELECT * FROM students WHERE dept = 'CSE'; -
Students with CGPA at least 3.5, sorted highest first.যাদের CGPA ৩.৫ বা বেশি — sorted, সবচেয়ে বেশি আগে।
✨ Show Answer
ans3.sqlSELECT name, cgpa FROM students WHERE cgpa >= 3.5 ORDER BY cgpa DESC; -
List all distinct cities customers come from.Customer-দের সব unique city দেখান।
✨ Show Answer
ans4.sqlSELECT DISTINCT city FROM customers ORDER BY city; -
Page 3 of orders, 4 per page, sorted by id.Order-এর তৃতীয় page (প্রতি page-এ ৪টি)।
✨ Show Answer
ans5.sqlSELECT * FROM orders ORDER BY id LIMIT 4 OFFSET 8; -
Find students with no recorded CGPA.যাদের CGPA এখনো নেই — তাদের দেখান।
✨ Show Answer
ans6.sqlSELECT name FROM students WHERE cgpa IS NULL; -
Cheapest 5 grocery items, ascending price.সবচেয়ে সস্তা ৫টি grocery item।
✨ Show Answer
ans7.sqlSELECT title, price FROM products WHERE category = 'grocery' ORDER BY price ASC LIMIT 5; -
Use
BETWEENto find students with CGPA between 3.0 and 3.5 inclusive.BETWEENদিয়ে CGPA ৩.০–৩.৫ এর মধ্যে student বের করুন।✨ Show Answer
ans8.sqlSELECT name, cgpa FROM students WHERE cgpa BETWEEN 3.0 AND 3.5; -
Use
INto filter by 3 departments at once.INদিয়ে তিনটি department একসাথে filter করুন।✨ Show Answer
ans9.sqlSELECT name, dept FROM students WHERE dept IN ('CSE', 'EEE', 'ME'); -
Why does
cgpa = NULLreturn zero rows? Two-sentence explanation.cgpa = NULLকেন কোনো row দেয় না? দুই বাক্যে।✨ Show Answer
Answer: NULL means "unknown," and any comparison with an unknown value is itself unknown — never TRUE. Since WHERE only keeps rows where the condition is TRUE, rows with NULL silently disappear.
NULL মানে "অজানা" — অজানার সাথে তুলনা করলে ফলও অজানা থাকে, কখনো TRUE হয় না। তাই সেই row WHERE-এর পরীক্ষায় বাদ পড়ে যায়।
-
List the top 3 most expensive products.সবচেয়ে দামি ৩টি product দেখান।
✨ Show Answer
ans11.sqlSELECT title, price FROM products ORDER BY price DESC LIMIT 3; -
Sort orders by status (asc) and within each status by amount (desc).Order-গুলো status (asc) এবং প্রতিটি status-এর মধ্যে amount (desc) দিয়ে sort করুন।
✨ Show Answer
ans12.sqlSELECT id, status, amount FROM orders ORDER BY status ASC, amount DESC; -
Use
DISTINCTon (dept, year) to list unique pairs.(dept, year) এর unique জোড়া বের করুন।✨ Show Answer
ans13.sqlSELECT DISTINCT dept, year FROM students ORDER BY dept, year; -
Why should every
LIMIThave anORDER BY? Two sentences.LIMIT-এর সাথেORDER BYকেন বাধ্যতামূলক — দুই বাক্যে।✨ Show Answer
Answer: Without ORDER BY, SQL is free to return rows in any order — which can change between executions. Pagination then becomes unstable: page 2 may overlap or skip rows that page 1 already returned.
ORDER BY ছাড়া SQL যেকোনো ক্রমে row ফেরত দিতে পারে, এবং সেই ক্রম বার বার বদলায়। তখন pagination অস্থির হয়ে যায় — page 2 এ আগেও দেখা row আবার আসতে পারে।
-
List items priced strictly between 100 and 1000 BDT (exclusive on both sides).দাম ১০০ থেকে ১০০০ টাকার মাঝে (exclusive) — সেই product-গুলো বের করুন।
✨ Show Answer
ans15.sqlSELECT title, price FROM products WHERE price > 100 AND price < 1000; -
Show every order alongside
amount * 1.05(5% VAT) astotal.প্রতিটি order-এর সাথেamount * 1.05(৫% VAT) দিন।✨ Show Answer
ans16.sqlSELECT id, amount, amount * 1.05 AS total FROM orders ORDER BY total DESC;
Summary — Module 13
A SELECT statement names columns (SELECT), the source (FROM), the
filter (WHERE), the sort (ORDER BY), and the page (LIMIT/OFFSET);
DISTINCT drops duplicates after selection. The database evaluates these clauses in a fixed
logical order, and NULL follows three-valued logic — always test it with
IS NULL, never = NULL. Master these six pieces and you can express most
day-to-day reads in a single statement.
SELECT-এর ছয়টি অংশ মনে রাখুন — column বাছাই, table (FROM), filter (WHERE), sort (ORDER BY), pagination (LIMIT/OFFSET) এবং DISTINCT। NULL সবসময় IS NULL দিয়ে পরীক্ষা করুন। এই ছয়টি দিয়ে দৈনন্দিন প্রায় সব read query লেখা যায়।