SQL Questions and Answers : PART 1
✅ SQL Date & Time Functions ✅
✅ SQL Date & Time Functions ✅
1. Why Date Functions Matter?
Almost every real-world database contains dates 👇
- ✔️ Orders
- ✔️ Employee joining dates
- ✔️ Transactions
- ✔️ Login activity
SQL date functions help analyze time-based data 💯
⚡ 2. Common Date Functions
Function : Purpose
NOW() : Current date & time
CURDATE() : Current date
CURTIME() : Current time
YEAR() : Extract year
MONTH() : Extract month
DAY() : Extract day
DATEDIFF() : Difference between dates
DATE_FORMAT() : Format dates
🔥 3. NOW(), CURDATE(), CURTIME()
SELECT NOW();
Output : ✔️ Current date + time
SELECT CURDATE();
Output : ✔️ Current date only
SELECT CURTIME();
Output : ✔️ Current time only
🔥 4. YEAR(), MONTH(), DAY()
SELECT YEAR(joining_date) FROM employees;
SELECT MONTH(joining_date) FROM employees;
SELECT DAY(joining_date) FROM employees;
🔥 5. DATEDIFF()
👉 Find difference between dates
SELECT DATEDIFF('2026-06-10', '2026-06-01');
✔️ Result → 9 days
🔥 6. DATE_FORMAT()
👉 Format dates professionally
SELECT DATE_FORMAT(NOW(), '%d-%m-%Y');
✔️ Example Output → 06-06-2026
🎯 7. Real Example
👉 Employees joined in 2025
SELECT * FROM employees WHERE YEAR(joining_date) = 2025;
🎯 8. Practice Tasks
1. Show current date
2. Show current time
3. Extract joining year from employee table
4. Find employees joined in specific month
5. Calculate days between two dates
⚡ Mini Challenge 🔥
👉 Find employees who joined in the last 30 days
✅ Solution Using DATEDIFF()
SELECT * FROM employees WHERE DATEDIFF(CURDATE(), joining_date) <= 30;
✅ Alternative Solution Using DATE_SUB()
SELECT * FROM employees WHERE joining_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);
🔥 Pro Tip
Date functions are heavily used in:
👉 Sales reports
👉 Monthly dashboards
👉 Retention analysis
👉 Time-series analytics 💯
✅ SQL String Functions
✅ SQL String Functions
🧠1. What are String Functions?
String functions are used to
- 👉 manipulate text data
- 👉 clean messy data
- 👉 format outputs
Used heavily in:
✔️ Data Analytics
✔️ Reporting
✔️ ETL processes
⚡ 2. Common String Functions
Function : Purpose
UPPER() : Convert to uppercase
LOWER() : Convert to lowercase
LENGTH() : Count characters
CONCAT() : Join strings
SUBSTRING() : Extract part of string
TRIM() : Remove spaces
REPLACE() : Replace text
🔥 3. UPPER() & LOWER()
SELECT UPPER(name) AS upper_name FROM employees;
SELECT LOWER(name) AS lower_name FROM employees;
🔥 4. LENGTH()
👉 Count number of characters
SELECT name, LENGTH(name) AS total_chars FROM employees;
🔥 5. CONCAT()
👉 Combine strings
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
🔥 6. SUBSTRING()
👉 Extract part of string
SELECT SUBSTRING(name, 1, 3) FROM employees;
✔️ Extracts first 3 characters
🔥 7. TRIM()
👉 Remove extra spaces
SELECT TRIM(' SQL ');
✔️ Result → SQL
🔥 8. REPLACE()
👉 Replace text inside string
SELECT REPLACE('I love Java', 'Java', 'SQL');
✔️ Result → I love SQL
🎯 9. Practice Tasks
1. Convert names to uppercase
2. Convert emails to lowercase
3. Combine first & last names
4. Extract first 4 letters of names
5. Remove extra spaces from city names
⚡ Mini Challenge 🔥
👉 Create employee usernames using:
first 3 letters of name + employee ID
Example:
Amit + 101 → Ami101
🔥 Mini Challenge Solution 💯
👉 Requirement:
Create username using:
- First 3 letters of name
- Employee ID
Example:
Amit + 101 → Ami101
✅ SQL Solution
SELECT name,
emp_id,
CONCAT(SUBSTRING(name, 1, 3), emp_id) AS username
FROM employees;
✅ Example Output
name : emp_id : username
Amit : 101 : Ami101
Neha : 102 : Neh102
Ravi : 103 : Rav103
🧠How It Works
👉 SUBSTRING(name, 1, 3)
Extracts first 3 letters
👉 CONCAT()
Combines extracted text with employee ID
🔥 Real-World Usage:
String functions are commonly used for:
👉 Username generation
👉 Email formatting
👉 Data cleaning
👉 Customer IDs 💯
COALESCE & NULL Handling in SQL
🧠1. What is NULL in SQL?
NULL means :
- 👉 missing value
- 👉 unknown value
- 👉 no data available
⚠️ NULL is NOT:
❌ 0
❌ Empty string
⚡ 2. Problems with NULL Values
NULL can affect:
- ❌ Calculations
- ❌ Comparisons
- ❌ Reports
Example 👇
SELECT salary + bonus
FROM employees;
If bonus is NULL → result becomes NULL ❌
========================================================
🔥 3. COALESCE Function
👉 Replaces NULL with another value
✅ Syntax
COALESCE(column, value)
⚡ 4. Basic Example
SELECT name,
COALESCE(bonus, 0) AS bonus
FROM employees;
✔️ If bonus is NULL → shows 0 instead
⚡ 5. Multiple Values with COALESCE
SELECT name,
COALESCE(phone, email, 'No Contact') AS contact
FROM employees;
✔️ Returns first non-NULL value
🔥 6. IS NULL & IS NOT NULL
👉 Find employees without bonus
SELECT * FROM employees
WHERE bonus IS NULL;
👉 Find employees with bonus
SELECT * FROM employees
WHERE bonus IS NOT NULL;
⚡ 7. NULL Handling in Aggregation
SELECT AVG(COALESCE(bonus, 0))
FROM employees;
✔️ Prevents NULL issues in calculations
🎯 8. Practice Tasks
1. Replace NULL bonus with 0
2. Find rows with NULL values
3. Find rows without NULL values
4. Use COALESCE with multiple columns
5. Calculate total salary safely using COALESCE
⚡ Mini Challenge 🔥
👉 Show employee total income:
salary + bonus
(If bonus is NULL → treat as 0)
🔥 Mini Challenge Solution 💯
👉 Requirement:
total_income = salary + bonus
If bonus is NULL → treat it as 0
✅ SQL Solution Using COALESCE
SELECT name,
salary,
COALESCE(bonus, 0) AS bonus,
salary + COALESCE(bonus, 0) AS total_income
FROM employees;
✅ Example Output
name : Amit, salary : 70000, bonus : 5000, total_income : 75000
name : Neha, salary : 50000, bonus : 0, total_income : 50000
name : Ravi, salary : 60000, bonus : 3000, total_income : 63000
🧠How It Works
👉 COALESCE(bonus, 0)
means:
- If bonus exists → use bonus
- If bonus is NULL → use 0
✅ CASE Statement in SQL
✅ CASE Statement in SQL
🧠1. What is CASE Statement?
- CASE is used for conditional logic in SQL
- It's like if-else in programming
- Helps categorize data
- Create custom labels
- Build smart reports
⚡ 2. Basic Syntax
SELECT column_name,
CASE
WHEN condition THEN result
ELSE result
END AS alias_name
FROM table_name;
📊 Example Table
name : Ajit, Neha, Sujit
salary : 70000, 40000, 55000
🔥 3. Basic CASE Example
👉 Categorize employees by salary
SELECT name, salary,
CASE
WHEN salary >= 60000 THEN 'High Salary'
WHEN salary >= 50000 THEN 'Medium Salary'
ELSE 'Low Salary'
END AS salary_category
FROM employees;
✅ Output
name : Ajit, Sujit, Neha
salary : 70000, 55000, 40000
salary_category : High Salary, Medium Salary, Low Salary
⚡ 4. CASE with Aggregation
👉 Count employees department-wise
SELECT department,
SUM(
CASE
WHEN salary > 50000 THEN 1
ELSE 0
END
) AS high_salary_count
FROM employees
GROUP BY department;
⚡ 5. CASE in ORDER BY
SELECT name, department
FROM employees
ORDER BY
CASE
WHEN department = 'IT' THEN 1
WHEN department = 'HR' THEN 2
ELSE 3
END;
🎯 6. Practice Tasks
1. Categorize salary as High/Low
2. Create age groups using CASE
3. Use CASE with GROUP BY
4. Count employees based on salary condition
5. Use CASE in ORDER BY
What will this query return?
SELECT name, salary,
CASE
WHEN salary >= 50000 THEN 'High'
ELSE 'Low'
END AS category
FROM employees;
A. Deletes salaries
B. Sorts employees
C. Add employees
D. Categorizes salary as High or Low
Ans : D
✅ UNION & UNION ALL in SQL
✅ UNION & UNION ALL in SQL
🧠1. What is UNION?
UNION is used to combine results from multiple SELECT queries
"Merge data from two tables into one result”
⚡ 2. Rules for UNION
- Same number of columns
- Same datatype/order of columns
📊 Example Tables
👨💼 employees_2025
Name
- Ajit
- Neha
👨💼 employees_2026
name
- Sujit
- Neha
🔥 3. UNION Example
SELECT name FROM employees_2025
UNION
SELECT name FROM employees_2025;
✔️ Removes duplicates automatically
✅ Result
name
- Ajit
- Neha
- Sujit
⚡ 4. UNION ALL
SELECT name FROM employees_2025
UNION ALL
SELECT name FROM employees_2026;
- Keeps duplicates
- Faster than UNION
✅ Result
name
- Ajit
- Neha
- Sujit
- Neha
🔥 5. UNION vs UNION ALL
UNION
- Removes duplicates
- Slower
- Doesn't keep all rows
UNION ALL
- Doesn't remove duplicates
- Faster
- Keeps all rows
⚡ 6. ORDER BY with UNION
SELECT name FROM employees_2025
UNION
SELECT name FROM employees_2026
ORDER BY name;
🎯 7. Practice Tasks
1. Combine employee names using UNION
2. Combine employee names using UNION ALL
3. Identify duplicate removal
4. Sort UNION result using ORDER BY
5. Compare UNION vs UNION ALL output
🧠Why UNION ALL?
👉 UNION → removes duplicates
👉 UNION ALL → keeps duplicates + faster
#union #union_alll