Here are a few additional concepts that could enhance further:
Useful for improving query readability and reusability.
WITH sales_summary AS (
SELECT department_id, SUM(salary) AS total_salary
FROM employees
GROUP BY department_id
)
SELECT *
FROM sales_summary
WHERE total_salary > 50000;
- Why Use? Simplifies complex queries and allows recursive operations.
Helpful for hierarchical data like organizational charts.
WITH RECURSIVE EmployeeHierarchy AS (
SELECT id, name, manager_id
FROM employees
WHERE manager_id IS NULL
UNION ALL
SELECT e.id, e.name, e.manager_id
FROM employees e
INNER JOIN EmployeeHierarchy eh ON e.manager_id = eh.id
)
SELECT * FROM EmployeeHierarchy;
For working with JSON data in SQL databases that support it (e.g., PostgreSQL, MySQL).
SELECT JSON_EXTRACT(data, '$.name') AS name
FROM employees;
SELECT LENGTH(name) FROM employees; -- Length of string
SELECT UPPER(name) FROM employees; -- Convert to uppercase
SELECT LOWER(name) FROM employees; -- Convert to lowercase
SELECT TRIM(' data ') FROM employees; -- Remove whitespace
SELECT SUBSTRING(name, 1, 3) FROM employees; -- Extract substring
SELECT NOW(); -- Current timestamp
SELECT CURDATE(); -- Current date
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d'); -- Format date
SELECT DATEDIFF(NOW(), '2023-01-01'); -- Days difference
SELECT TIMESTAMPDIFF(YEAR, birth_date, NOW()); -- Age calculation
SELECT
department,
SUM(CASE WHEN gender = 'Male' THEN salary ELSE 0 END) AS male_salary,
SUM(CASE WHEN gender = 'Female' THEN salary ELSE 0 END) AS female_salary
FROM employees
GROUP BY department;
Example for SQL Server:
BEGIN TRY
INSERT INTO employees (id, name, salary) VALUES (1, 'John', 50000);
END TRY
BEGIN CATCH
PRINT 'An error occurred!';
END CATCH
START TRANSACTION;
UPDATE employees
SET salary = salary * 1.10
WHERE department = 'Sales';
COMMIT; -- Save changes
-- ROLLBACK; -- Undo changes if something goes wrong
CREATE POLICY employee_policy
ON employees
FOR SELECT
USING (department = current_department);
- Purpose: Restricts data access based on roles.
SELECT name, salary,
salary * 100.0 / SUM(salary) OVER () AS percentage_of_total
FROM employees;
SELECT name, salary,
NTILE(4) OVER (ORDER BY salary) AS quartile
FROM employees;
CREATE FUNCTION get_bonus(salary DECIMAL(10,2))
RETURNS DECIMAL(10,2)
BEGIN
RETURN salary * 0.10; -- Bonus = 10% of salary
END;
SELECT name, salary, get_bonus(salary) AS bonus
FROM employees;
GRANT SELECT, INSERT ON employees TO 'user1'@'localhost';
REVOKE SELECT ON employees FROM 'user1'@'localhost';
CREATE INDEX idx_name ON employees(name);
EXPLAIN SELECT * FROM employees WHERE salary > 50000;
mysqldump -u username -p database_name > backup.sql
mysql -u username -p database_name < backup.sql
- Add real-world query examples based on datasets (e.g., employees, sales).
- Include SQL best practices for writing readable and efficient queries.
- Provide links to tools like SQL Fiddle or DB Fiddle for interactive testing.
- Add interview-specific SQL queries like the second-highest salary or duplicate removal tasks.
CompiledByUdithaWICK