SQL Short Notes Top 10 Questions 1748266007
SQL Short Notes Top 10 Questions 1748266007
2. Data Types
• INT, VARCHAR(n), DATE, DECIMAL(p, s), BOOLEAN, etc.
3. Basic Commands
• SELECT: Retrieve data
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()
Joins
11. Types of Joins
• INNER JOIN: Matches in both tables
FROM employees a
12. GROUP BY
Groups rows by column
GROUP BY department
13. HAVING
14. Subquery
FROM employees
Advanced Concepts
FROM employees;
CASE
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
Q1: How Would You Identify Top & Bottom Performing Products?
WITH ranked AS (
SELECT product_id,
SUM(sales) AS total_sales,
FROM sales_data
GROUP BY product_id
SELECT *
FROM ranked
FROM orders
COUNT(*) AS total_orders,
(SUM(CASE WHEN is_returned = TRUE THEN 1 ELSE 0 END) * 100.0 / COUNT(*)) AS return_rate
FROM orders
GROUP BY product_id;
FROM (
FROM orders
) AS sub;
FROM orders
GROUP BY customer_id
FROM customers;
Q7: How Do You Create a Percentile-Based Bucket
WITH ranked AS (
FROM orders
GROUP BY customer_id
Q8: Write a query to fetch the second-highest salary from an employee table.
SELECT MAX(salary) AS second_highest_salary
FROM employee
OR
FROM employee
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
ON c.customer_id = o.customer_id
• 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.
• Use explicit JOIN syntax (INNER JOIN, LEFT JOIN) rather than old-style joins in WHERE.