0% found this document useful (0 votes)
23 views8 pages

SQL Short Notes Top 10 Questions 1748266007

This document provides an overview of SQL basics, including data types, commands, filtering, sorting, functions, joins, and advanced concepts like window functions and CTEs. It also includes ten common SQL questions with example queries that cover topics such as identifying top products, detecting duplicates, and optimizing query performance. Key SQL features such as aggregate functions, constraints, and indexing are highlighted throughout the notes.

Uploaded by

Md Muqeet Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

SQL Short Notes Top 10 Questions 1748266007

This document provides an overview of SQL basics, including data types, commands, filtering, sorting, functions, joins, and advanced concepts like window functions and CTEs. It also includes ten common SQL questions with example queries that cover topics such as identifying top products, detecting duplicates, and optimizing query performance. Key SQL features such as aggregate functions, constraints, and indexing are highlighted throughout the notes.

Uploaded by

Md Muqeet Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SQL

Short Notes + Top 10


Questions

Nitish Kumar Yadav


SQL Basics
1. What is SQL?
• SQL (Structured Query Language) is used to manage and manipulate relational databases.

2. Data Types
• INT, VARCHAR(n), DATE, DECIMAL(p, s), BOOLEAN, etc.

3. Basic Commands
• SELECT: Retrieve data

• INSERT INTO: Add new records

• UPDATE: Modify existing records

• DELETE: Remove records

• CREATE TABLE: Create new table

• DROP TABLE: Delete a table

Filtering & Sorting


4. WHERE Clause
• Filters records:
SELECT * FROM employees WHERE salary > 50000;

5. Operators
• = , !=, <, >, BETWEEN, IN, LIKE, IS NULL
6. ORDER BY
• Sorts result set:
ORDER BY salary DESC;

7. LIMIT / TOP
• Restrict number of rows:
LIMIT 10; (MySQL), TOP 5 (SQL Server)
Functions
8. Aggregate Functions
• COUNT(), SUM(), AVG(), MAX(), MIN()

9. String Functions
• CONCAT(), UPPER(), LOWER(), SUBSTRING(), TRIM()

10. Date Functions


• NOW(), CURDATE(), DATEDIFF(), MONTH(), YEAR()

Joins
11. Types of Joins
• INNER JOIN: Matches in both tables

• LEFT JOIN: All from left + matches

• RIGHT JOIN: All from right + matches

• FULL OUTER JOIN: All records from both

• SELF JOIN: Joining a table with itself

SELECT a.name, b.name

FROM employees a

JOIN employees b ON a.manager_id = b.id;

Grouping & Filtering

12. GROUP BY
Groups rows by column

GROUP BY department

13. HAVING

Filters grouped data:


HAVING COUNT(*) > 1
Subqueries & CTEs

14. Subquery

Query inside another query


SELECT name

FROM employees

WHERE salary > (SELECT AVG(salary) FROM employees);

15. CTE (WITH clause)

Temporary result set for reuse


WITH HighEarners AS (

SELECT * FROM employees WHERE salary > 80000

SELECT * FROM HighEarners;

Advanced Concepts

16. Window Functions


ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), LAG()

SELECT name, salary,

RANK() OVER (PARTITION BY department ORDER BY salary DESC)

FROM employees;

17. CASE Statement


SELECT name,

CASE

WHEN salary > 80000 THEN 'High'

WHEN salary > 50000 THEN 'Medium'

ELSE 'Low'

END AS salary_category
FROM employees;
Indexes, Views, and Constraints

18. Indexes
Speed up searches, e.g. CREATE INDEX idx_name ON employees(name);

19. Views

Virtual tables:
CREATE VIEW high_paid AS SELECT * FROM employees WHERE salary > 100000;

20. Constraints

PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE, CHECK, DEFAULT

Q1: How Would You Identify Top & Bottom Performing Products?
WITH ranked AS (

SELECT product_id,

SUM(sales) AS total_sales,

PERCENT_RANK() OVER (ORDER BY SUM(sales)) AS p_rank

FROM sales_data

GROUP BY product_id

SELECT *

FROM ranked

WHERE p_rank <= 0.05 OR p_rank >= 0.95;

Q2: How Do You Detect Duplicate Records Across Multiple Columns?


SELECT user_id, order_date, COUNT(*)

FROM orders

GROUP BY user_id, order_date

HAVING COUNT(*) > 1;


Q3: How Would You Analyze Product Return Rates?
SELECT product_id,

COUNT(*) AS total_orders,

SUM(CASE WHEN is_returned = TRUE THEN 1 ELSE 0 END) AS Returns,

(SUM(CASE WHEN is_returned = TRUE THEN 1 ELSE 0 END) * 100.0 / COUNT(*)) AS return_rate

FROM orders

GROUP BY product_id;

Q4 : Rank Products Within Each Category by Sales


SELECT product_id, category,

RANK() OVER (PARTITION BY category ORDER BY total_sales DESC) AS sales_rank

FROM (

SELECT product_id, category, SUM(sales) AS total_sales

FROM orders

GROUP BY product_id, category

) AS sub;

Q5: Find the Top N% of Customers by Spend


WITH ranked AS (

SELECT customer_id, SUM(sales) AS total_sales,

NTILE(100) OVER (ORDER BY SUM(sales) DESC) AS percentile

FROM orders

GROUP BY customer_id

SELECT * FROM ranked WHERE percentile <= N;

Q6: Explain the Use of CASE WHEN Inside Aggregations


SELECT COUNT(*) AS total,

COUNT(CASE WHEN status = 'active' THEN 1 END) AS active_count

SUM(CASE WHEN status = 'inactive' THEN sales ELSE 0 END) AS inactive_sales

FROM customers;
Q7: How Do You Create a Percentile-Based Bucket
WITH ranked AS (

SELECT customer_id, SUM(sales) AS total_spent,

NTILE(4) OVER (ORDER BY SUM(sales) DESC) AS quartile

FROM orders

GROUP BY customer_id

SELECT * FROM ranked WHERE quartile = 1;

Q8: Write a query to fetch the second-highest salary from an employee table.
SELECT MAX(salary) AS second_highest_salary

FROM employee

WHERE salary < (SELECT MAX(salary) FROM employee);

OR

SELECT DISTINCT salary

FROM employee

ORDER BY salary DESC

LIMIT 1 OFFSET 1;

Q9:Write a query to find all customers who have not made any purchases in
the last 6 months.
SELECT c.customer_id, c.customer_name

FROM customers c

LEFT JOIN orders o

ON c.customer_id = o.customer_id

AND o.order_date >= CURRENT_DATE - INTERVAL '6 months'

WHERE o.order_id IS NULL;


Q10:How do you optimize SQL queries for better performance?
1. Use Indexes Wisely

• Create indexes on columns that are frequently used in WHERE, JOIN, ORDER BY, and GROUP
BY clauses.

• Avoid over-indexing because indexes slow down INSERT/UPDATE operations and consume
storage.

2. Write Selective Queries

• Use filtering with WHERE clauses to reduce data scanned.

• Avoid SELECT *; instead, select only the columns you need.

3. Use Joins Properly

• Use explicit JOIN syntax (INNER JOIN, LEFT JOIN) rather than old-style joins in WHERE.

• Join only on indexed columns.

• Filter rows before joins if possible.

4. Use Pagination Techniques

• For large result sets, use LIMIT/OFFSET

You might also like