Session 02 ยท Phase 1: Foundations & SQL

SQL Basics โ€” SELECT, WHERE, ORDER BY & Operators

The foundation of every data analyst interview: retrieving the right rows and columns from a database, filtering with conditions, and sorting the results.

โฑ ~2 hrs ๐Ÿ“š Core content ๐ŸŽฏ High priority

Learning Objectives

0. The Sample Table Used Throughout

Every example below uses this simple customers table. Read it once before moving on โ€” the queries will make far more sense when you can picture the data.

customer_idcustomer_namecityaccount_typebalancejoin_date
1Aarav SharmaMumbaiSavings250002023-01-15
2Priya VermaDelhiCurrent1200002022-06-30
3Rahul MehtaMumbaiSavings80002024-03-10
4Sneha IyerBengaluruSalary950002021-11-22
5Vikram SinghDelhiCurrent450002023-09-05
6Ananya DasChennaiSavings150002022-02-14
๐Ÿ“
Note: This is a generic "banking" style dataset (accounts and balances). You can recreate it in any SQL environment โ€” SQLite, MySQL, PostgreSQL โ€” to follow along.

1. The SELECT Statement

SELECT is how you retrieve data. Its job is to decide which columns you want to see. Think of it as choosing the vertical slices of a table.

Selecting specific columns

-- Choose just the name and balance of every customer
SELECT customer_name, balance
FROM customers;

Selecting all columns

-- The asterisk (*) means "every column"
SELECT *
FROM customers;
โš ๏ธ
Warning: SELECT * is fine for a quick look, but avoid it in real analysis and interviews. Selecting only the columns you need is faster and clearer โ€” interviewers specifically listen for this.

Giving a column a friendly name (alias)

SELECT customer_name AS name,
       balance
FROM customers;

Use AS to rename a column in the output. It doesn't change the table โ€” only the display.

2. Filtering Rows with WHERE

WHERE decides which rows to keep. While SELECT picks columns, WHERE picks rows based on a condition.

-- Customers from Mumbai only
SELECT customer_name, city
FROM customers
WHERE city = 'Mumbai';

Comparison operators

OperatorMeaningExample
=equal tocity = 'Mumbai'
<> or !=not equal tocity <> 'Mumbai'
>greater thanbalance > 50000
<less thanbalance < 20000
>=greater than or equalbalance >= 50000
<=less than or equalbalance <= 20000
-- Customers with a balance above 50,000
SELECT customer_name, balance
FROM customers
WHERE balance > 50000;

-- Customers who joined in 2023 or later
SELECT customer_name, join_date
FROM customers
WHERE join_date >= '2023-01-01';
๐ŸŒ
Real World: "Which customers have a balance over 50,000?" is exactly the kind of one-line request a business stakeholder makes. WHERE is your answer.

3. Logical Operators โ€” AND, OR, NOT

Combine multiple conditions in a single WHERE clause.

AND โ€” both conditions must be true

-- Mumbai customers with a balance above 20,000
SELECT customer_name, city, balance
FROM customers
WHERE city = 'Mumbai'
  AND balance > 20000;

OR โ€” at least one condition must be true

-- Customers in Mumbai OR Delhi
SELECT customer_name, city
FROM customers
WHERE city = 'Mumbai'
   OR city = 'Delhi';

NOT โ€” reverses a condition

-- Customers NOT in Mumbai
SELECT customer_name, city
FROM customers
WHERE NOT city = 'Mumbai';
๐Ÿ’ก
Pro Tip: When mixing AND and OR, use parentheses to make the logic unambiguous. WHERE (city = 'Mumbai' OR city = 'Delhi') AND balance > 20000 means something different from WHERE city = 'Mumbai' OR (city = 'Delhi' AND balance > 20000).

4. IN, BETWEEN, and LIKE

IN โ€” match any value in a list

Much cleaner than writing a long chain of ORs.

-- Customers in Mumbai, Delhi, or Chennai
SELECT customer_name, city
FROM customers
WHERE city IN ('Mumbai', 'Delhi', 'Chennai');

BETWEEN โ€” match a range (inclusive)

-- Customers with a balance between 20,000 and 60,000
SELECT customer_name, balance
FROM customers
WHERE balance BETWEEN 20000 AND 60000;

BETWEEN is inclusive โ€” both 20,000 and 60,000 are included.

LIKE โ€” match a pattern with wildcards

WildcardMeaning
%any sequence of zero or more characters
_exactly one character
-- Names starting with 'A'
SELECT customer_name
FROM customers
WHERE customer_name LIKE 'A%';

-- Names ending with 'Sharma'
SELECT customer_name
FROM customers
WHERE customer_name LIKE '%Sharma';
๐Ÿ“
Note: LIKE is case-insensitive in some databases (MySQL) and case-sensitive in others (PostgreSQL). Don't assume โ€” test it in your environment.

5. Handling NULL Values

NULL means "missing" or "unknown". It is not the same as zero or an empty string. This is one of the most common interview traps.

โš ๏ธ
Warning: You cannot compare NULL with =. WHERE balance = NULL will not work. You must use IS NULL or IS NOT NULL.
-- Find rows where a column is missing
SELECT customer_name
FROM customers
WHERE city IS NULL;

-- Find rows where a column has a value
SELECT customer_name
FROM customers
WHERE city IS NOT NULL;

6. ORDER BY, DISTINCT, and LIMIT

ORDER BY โ€” sort the results

Default is ascending (ASC); use DESC for descending.

-- Highest balance first
SELECT customer_name, balance
FROM customers
ORDER BY balance DESC;

-- Sort by city (Aโ†’Z), then by balance (highโ†’low) within each city
SELECT city, customer_name, balance
FROM customers
ORDER BY city ASC, balance DESC;

DISTINCT โ€” remove duplicates

-- List each unique city exactly once
SELECT DISTINCT city
FROM customers;

Without DISTINCT, "Mumbai" and "Delhi" would appear twice. With it, each city appears once.

LIMIT โ€” return only the top N rows

-- The 3 highest-balance customers
SELECT customer_name, balance
FROM customers
ORDER BY balance DESC
LIMIT 3;

LIMIT is standard in MySQL, PostgreSQL, and SQLite. SQL Server uses SELECT TOP 3 ... instead โ€” know both, because the question changes by database.

๐Ÿ“‹ Stable content โ€” Reviewed: August 2026

7. Interview Questions (with Model Answers)

These are the beginner SQL questions most commonly asked to Data Analyst freshers, mapped to this session's topics. Try to answer each one before opening the reveal โ€” self-testing is how you remember.

IQ1. What is SQL, and why is it important for a data analyst?

Why they ask: To check you understand the tool at a conceptual level, not just syntax.

Model answer: "SQL โ€” Structured Query Language โ€” is the standard language for working with data in relational databases. As a data analyst, I use it to extract specific rows and columns, filter records, join tables, and summarize data for reports and dashboards. It lets me work directly with the source data instead of relying on exported spreadsheets."

IQ2. What is the difference between SELECT and WHERE?

Why they ask: This is the single most common beginner question โ€” it tests whether you understand rows vs columns.

Model answer: "SELECT chooses which columns to show, while WHERE filters which rows to keep. For example, SELECT customer_name FROM customers WHERE city = 'Mumbai' selects only the name column, and only for rows where the city is Mumbai."

IQ3. How do you select all columns from a table, and is it a good idea?

Why they ask: They want to hear that you know the performance/readability trade-off.

Model answer: "I'd use SELECT * FROM customers to see everything. But in real analysis I avoid SELECT * on large tables, because selecting only the columns I need is faster, uses less memory, and makes the query clearer to others."

IQ4. How do you find unique values in a column?

Why they ask: Tests your knowledge of DISTINCT โ€” used constantly for counts and categories.

Model answer: "I use SELECT DISTINCT city FROM customers. DISTINCT removes duplicates, so each city appears only once."

IQ5. How do you sort results from highest to lowest?

Why they ask: Checks ORDER BY and the DESC keyword โ€” used in nearly every report.

Model answer: "I'd use ORDER BY balance DESC. ORDER BY sorts the rows, and DESC sorts them in descending order โ€” highest first."

IQ6. What operators can you use in a WHERE clause to filter data?

Why they ask: To see how complete your filtering vocabulary is.

Model answer: "Comparison operators like =, <>, >, <, >=, <=; logical operators AND, OR, NOT; and special operators IN for lists, BETWEEN for ranges, and LIKE for patterns."

IQ7. Why does WHERE column = NULL not work? What should you use?

Why they ask: NULL handling is a classic trap that separates careful candidates from the rest.

Model answer: "NULL means an unknown or missing value, so it can't be compared with =. Instead, I use IS NULL or IS NOT NULL โ€” for example, WHERE city IS NULL."

IQ8. What's the difference between WHERE and HAVING? (Preview)

Why they ask: It's one of the most frequently asked SQL interview questions overall.

Model answer (short version): "WHERE filters rows before any grouping/aggregation, while HAVING filters after grouping. So WHERE works on individual rows, and HAVING works on aggregated results like SUM or COUNT."

Note: This is covered in depth in Session 04 (Aggregations). Knowing the one-line answer now is enough.

๐Ÿ’ก
How to answer SQL questions in an interview: Don't jump straight to the query. First restate the business question, say which table/columns you need, then write the query, then explain what the output means. This "think out loud" habit scores heavily with interviewers.

Hands-On Project: Query the Customers Table

Recreate the customers table in your SQL environment, then write queries to answer each business question below. Write the query and the expected result before you run it โ€” predicting output is the real skill.

Steps

  1. Create the table and insert the 6 rows shown in Section 0.
  2. Write a query to list all customers in Mumbai.
  3. Write a query to list customers with a balance greater than 30,000.
  4. Write a query to list customers with a balance between 20,000 and 100,000.
  5. Write a query to list all unique account_type values.
  6. Write a query to list the top 2 customers by balance.
  7. Write a query to list customers whose name starts with the letter 'A'.
View Solution / Walkthrough
-- 2. Customers in Mumbai
SELECT customer_name
FROM customers
WHERE city = 'Mumbai';
-- โ†’ Aarav Sharma, Rahul Mehta

-- 3. Balance greater than 30,000
SELECT customer_name, balance
FROM customers
WHERE balance > 30000;
-- โ†’ Priya Verma (120000), Sneha Iyer (95000), Vikram Singh (45000)

-- 4. Balance between 20,000 and 100,000
SELECT customer_name, balance
FROM customers
WHERE balance BETWEEN 20000 AND 100000;
-- โ†’ Aarav Sharma (25000), Sneha Iyer (95000), Vikram Singh (45000)

-- 5. Unique account types
SELECT DISTINCT account_type
FROM customers;
-- โ†’ Savings, Current, Salary

-- 6. Top 2 customers by balance
SELECT customer_name, balance
FROM customers
ORDER BY balance DESC
LIMIT 2;
-- โ†’ Priya Verma (120000), Sneha Iyer (95000)

-- 7. Names starting with 'A'
SELECT customer_name
FROM customers
WHERE customer_name LIKE 'A%';
-- โ†’ Aarav Sharma, Ananya Das

Key Takeaways

1

SELECT picks columns; WHERE picks rows. Keep the two straight.

2

Use IN, BETWEEN, and LIKE for cleaner, more readable filters.

3

NULL is "unknown", not zero โ€” compare it with IS NULL, never =.

4

ORDER BY sorts, DISTINCT de-duplicates, LIMIT caps the rows.

5

In interviews, explain your logic out loud โ€” "think out loud" beats a silent correct query.

Objective Questions โ€” Test Your Understanding

Q1. Which clause is used to filter rows based on a condition?

Q2. Which operator is used to check if a value lies within a range (inclusive)?

Q3. How do you correctly filter rows where a column has a missing (NULL) value?

Q4. Which wildcard matches any sequence of characters in a LIKE pattern?

Q5. Which query returns the 5 highest-balance customers?