Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views20 pages

100+ SQL Interview Questions

The document contains over 100 SQL interview questions categorized into basic and intermediate levels, covering essential concepts such as SQL commands, JOIN types, aggregate functions, subqueries, and window functions. It provides explanations, differences between similar concepts, and example queries for practical understanding. The content is designed to help candidates prepare for SQL-related interviews effectively.

Uploaded by

gargprakhar2016
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)
10 views20 pages

100+ SQL Interview Questions

The document contains over 100 SQL interview questions categorized into basic and intermediate levels, covering essential concepts such as SQL commands, JOIN types, aggregate functions, subqueries, and window functions. It provides explanations, differences between similar concepts, and example queries for practical understanding. The content is designed to help candidates prepare for SQL-related interviews effectively.

Uploaded by

gargprakhar2016
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/ 20

100+ SQL Interview Questions for Interviews

Basic Level Questions (1-35)

1. What is SQL?
SQL (Structured Query Language) is a standard programming language designed for managing and
manipulating relational databases. It allows users to create, read, update, and delete data.

2. What are the different types of SQL commands?


DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE

DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE

DCL (Data Control Language): GRANT, REVOKE

TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT

3. What is the difference between DELETE and TRUNCATE?


DELETE: Removes rows based on conditions, can be rolled back, slower, fires triggers

TRUNCATE: Removes all rows, cannot be rolled back, faster, doesn't fire triggers

4. What is the difference between DROP and DELETE?


DROP: Removes the entire table structure and data

DELETE: Removes only the data/rows from the table

5. What are the different types of JOINs in SQL?


INNER JOIN: Returns matching records from both tables

LEFT JOIN: Returns all records from left table and matching from right

RIGHT JOIN: Returns all records from right table and matching from left

FULL OUTER JOIN: Returns all records when there's a match in either table

CROSS JOIN: Returns Cartesian product of both tables

6. Write a query to find the second highest salary from an Employee table.

sql

SELECT MAX(salary) as SecondHighest


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

-- Alternative using LIMIT/OFFSET


SELECT salary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET 1;

7. What is the difference between WHERE and HAVING clauses?


WHERE: Filters rows before grouping, cannot use aggregate functions

HAVING: Filters groups after grouping, can use aggregate functions

8. What is a PRIMARY KEY?


A PRIMARY KEY is a constraint that uniquely identifies each record in a table. It cannot contain NULL
values and must be unique.

9. What is a FOREIGN KEY?


A FOREIGN KEY is a constraint that establishes a link between two tables by referencing the PRIMARY KEY
of another table.

10. What is the difference between UNION and UNION ALL?


UNION: Combines results and removes duplicates
UNION ALL: Combines results and keeps all duplicates

11. What are aggregate functions in SQL?


Aggregate functions perform calculations on multiple rows and return a single value:

COUNT(), SUM(), AVG(), MIN(), MAX()

12. What is GROUP BY clause?


GROUP BY groups rows with the same values in specified columns into summary rows, often used with
aggregate functions.

13. What is ORDER BY clause?


ORDER BY sorts the result set in ascending (ASC) or descending (DESC) order based on specified
columns.

14. What is the difference between CHAR and VARCHAR?


CHAR: Fixed-length string, pads with spaces
VARCHAR: Variable-length string, no padding

15. What are NULL values and how do you handle them?
NULL represents missing or unknown data. Use IS NULL or IS NOT NULL to check for NULL values, and
functions like COALESCE() or ISNULL() to handle them.

16. Write a query to find duplicate records in a table.

sql

SELECT column_name, COUNT(*)


FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;

17. What is the difference between COUNT(*) and COUNT(column_name)?


COUNT(*): Counts all rows including NULLs

COUNT(column_name): Counts non-NULL values in the specified column

18. What is a subquery?


A subquery is a query nested inside another query. It can be used in SELECT, FROM, WHERE, or HAVING
clauses.

19. What are the types of subqueries?


Scalar subquery: Returns a single value

Row subquery: Returns a single row

Column subquery: Returns a single column


Table subquery: Returns multiple rows and columns

20. What is the difference between IN and EXISTS?


IN: Compares a value with a list of values

EXISTS: Checks if a subquery returns any rows (boolean result)

21. What is CASE statement in SQL?


CASE statement provides conditional logic in SQL queries:

sql

CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE result3
END

22. What are SQL constraints?


Constraints are rules applied to table columns to ensure data integrity:

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

23. What is an INDEX?


An INDEX is a database object that improves query performance by creating a faster path to data
retrieval.

24. Write a query to find employees with salary greater than average salary.

sql

SELECT *
FROM Employee
WHERE salary > (SELECT AVG(salary) FROM Employee);

25. What is the difference between INNER JOIN and LEFT JOIN?
INNER JOIN: Returns only matching records from both tables
LEFT JOIN: Returns all records from left table and matching from right table

26. How do you remove duplicates from a table?


sql

-- Using ROW_NUMBER()
DELETE FROM table_name
WHERE id NOT IN (
SELECT MIN(id)
FROM table_name
GROUP BY duplicate_column
);

-- Using DISTINCT with CREATE TABLE


CREATE TABLE temp_table AS
SELECT DISTINCT * FROM original_table;

27. What is DISTINCT keyword?


DISTINCT removes duplicate rows from the result set, returning only unique records.

28. What is the difference between BETWEEN and IN operators?


BETWEEN: Checks if a value is within a range
IN: Checks if a value matches any value in a list

29. What are wildcards in SQL?


Wildcards are used with LIKE operator for pattern matching:

%: Represents zero or more characters


_: Represents exactly one character

30. Write a query to find the Nth highest salary.

sql

-- Using DENSE_RANK()
SELECT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) as rank
FROM Employee
) ranked
WHERE rank = N;

-- Using LIMIT with OFFSET


SELECT DISTINCT salary
FROM Employee
ORDER BY salary DESC
LIMIT 1 OFFSET N-1;

31. What is the difference between RANK() and DENSE_RANK()?


RANK(): Gaps in ranking when there are ties (1,2,2,4)

DENSE_RANK(): No gaps in ranking when there are ties (1,2,2,3)

32. What is ROW_NUMBER() function?


ROW_NUMBER() assigns a unique sequential integer to each row within a partition of a result set.

33. What is LIMIT clause?


LIMIT restricts the number of rows returned by a query (MySQL, PostgreSQL). In SQL Server, use TOP.

34. What is the difference between DELETE and TRUNCATE in terms of transaction?
DELETE: Can be rolled back, part of transaction log

TRUNCATE: Cannot be rolled back (in most databases), minimal logging

35. Write a query to find common records between two tables.

sql

SELECT * FROM table1


INTERSECT
SELECT * FROM table2;

-- Alternative using INNER JOIN


SELECT DISTINCT t1.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.id = t2.id;

Intermediate Level Questions (36-75)

36. What are Window Functions?


Window functions perform calculations across a set of rows related to the current row without grouping
the result set.

37. Explain PARTITION BY clause.


PARTITION BY divides the result set into partitions and performs calculations within each partition
independently.

38. Write a query to find running total using window functions.

sql

SELECT
employee_id,
salary,
SUM(salary) OVER (ORDER BY employee_id) as running_total
FROM Employee;

39. What is the difference between RANK(), DENSE_RANK(), and ROW_NUMBER()?

sql

SELECT
name,
salary,
RANK() OVER (ORDER BY salary DESC) as rank_val,
DENSE_RANK() OVER (ORDER BY salary DESC) as dense_rank_val,
ROW_NUMBER() OVER (ORDER BY salary DESC) as row_num
FROM Employee;

40. How do you find the difference between consecutive rows?


sql

SELECT
employee_id,
salary,
salary - LAG(salary) OVER (ORDER BY employee_id) as salary_diff
FROM Employee;

41. What are LEAD() and LAG() functions?


LAG(): Accesses data from previous row

LEAD(): Accesses data from next row

42. Write a query to find top N records from each group.

sql

SELECT * FROM (
SELECT
*,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) as rn
FROM Employee
) ranked
WHERE rn <= N;

43. What is CTE (Common Table Expression)?


CTE is a temporary named result set that exists within the scope of a single SQL statement:

sql

WITH employee_cte AS (
SELECT department, AVG(salary) as avg_salary
FROM Employee
GROUP BY department
)
SELECT * FROM employee_cte WHERE avg_salary > 50000;

44. What is a recursive CTE?


A recursive CTE references itself to process hierarchical or tree-structured data:

sql

WITH RECURSIVE employee_hierarchy AS (


SELECT employee_id, name, manager_id, 1 as level
FROM Employee
WHERE manager_id IS NULL

UNION ALL

SELECT e.employee_id, e.name, e.manager_id, eh.level + 1


FROM Employee e
JOIN employee_hierarchy eh ON e.manager_id = eh.employee_id
)
SELECT * FROM employee_hierarchy;

45. How do you pivot data in SQL?


sql

-- Using CASE statements


SELECT
department,
SUM(CASE WHEN year = 2021 THEN revenue ELSE 0 END) as "2021",
SUM(CASE WHEN year = 2022 THEN revenue ELSE 0 END) as "2022"
FROM sales
GROUP BY department;

-- Using PIVOT (SQL Server)


SELECT department, [2021], [2022]
FROM (SELECT department, year, revenue FROM sales) as src
PIVOT (SUM(revenue) FOR year IN ([2021], [2022])) as pvt;

46. Write a query to find employees who earn more than their manager.

sql

SELECT e1.name as employee, e2.name as manager


FROM Employee e1
JOIN Employee e2 ON e1.manager_id = e2.employee_id
WHERE e1.salary > e2.salary;

47. How do you handle NULL values in aggregations?

sql

-- COUNT ignores NULLs


SELECT COUNT(column_name) FROM table_name;

-- Use COALESCE or ISNULL to replace NULLs


SELECT AVG(COALESCE(salary, 0)) FROM Employee;

-- Filter out NULLs explicitly


SELECT AVG(salary) FROM Employee WHERE salary IS NOT NULL;

48. What is the difference between HAVING and WHERE with GROUP BY?

sql

-- WHERE filters before grouping


SELECT department, COUNT(*)
FROM Employee
WHERE salary > 30000
GROUP BY department;

-- HAVING filters after grouping


SELECT department, COUNT(*)
FROM Employee
GROUP BY department
HAVING COUNT(*) > 5;

49. Write a query to find departments with more than 5 employees.


sql

SELECT department, COUNT(*) as employee_count


FROM Employee
GROUP BY department
HAVING COUNT(*) > 5;

50. How do you find missing sequence numbers?

sql

WITH RECURSIVE numbers AS (


SELECT 1 as num
UNION ALL
SELECT num + 1 FROM numbers WHERE num < 100
)
SELECT num
FROM numbers
WHERE num NOT IN (SELECT id FROM your_table);

51. What is CROSS APPLY and OUTER APPLY?


These are SQL Server specific operators:

CROSS APPLY: Similar to INNER JOIN with table-valued functions

OUTER APPLY: Similar to LEFT JOIN with table-valued functions

52. Write a query to find the median value.

sql

-- Using window functions


SELECT DISTINCT
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary)
OVER () as median_salary
FROM Employee;

-- Alternative method
WITH ordered_salaries AS (
SELECT salary, ROW_NUMBER() OVER (ORDER BY salary) as row_num,
COUNT(*) OVER () as total_count
FROM Employee
)
SELECT AVG(salary) as median_salary
FROM ordered_salaries
WHERE row_num IN ((total_count + 1) / 2, (total_count + 2) / 2);

53. How do you find gaps in sequential data?


sql

SELECT
id + 1 as gap_start,
next_id - 1 as gap_end
FROM (
SELECT id, LEAD(id) OVER (ORDER BY id) as next_id
FROM your_table
) t
WHERE next_id - id > 1;

54. What is the difference between CHAR_LENGTH and LENGTH?


CHAR_LENGTH/CHARACTER_LENGTH: Returns number of characters

LENGTH: Returns number of bytes (may differ for multi-byte characters)

55. Write a query to concatenate strings from multiple rows.

sql

-- SQL Server
SELECT department,
STRING_AGG(name, ', ') as employees
FROM Employee
GROUP BY department;

-- MySQL
SELECT department,
GROUP_CONCAT(name SEPARATOR ', ') as employees
FROM Employee
GROUP BY department;

-- PostgreSQL
SELECT department,
STRING_AGG(name, ', ') as employees
FROM Employee
GROUP BY department;

56. How do you perform date arithmetic in SQL?

sql

-- Add/subtract days
SELECT DATE_ADD(current_date, INTERVAL 30 DAY);
SELECT current_date + INTERVAL '30 days';

-- Find difference between dates


SELECT DATEDIFF(date1, date2);
SELECT date1 - date2;

-- Extract parts of date


SELECT EXTRACT(YEAR FROM date_column);
SELECT YEAR(date_column);

57. What is MERGE statement?


MERGE performs INSERT, UPDATE, or DELETE operations in a single statement based on a condition:
sql

MERGE target_table AS target


USING source_table AS source
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET target.value = source.value
WHEN NOT MATCHED THEN
INSERT (id, value) VALUES (source.id, source.value);

58. Write a query to find employees hired in the last 30 days.

sql

SELECT *
FROM Employee
WHERE hire_date >= CURRENT_DATE - INTERVAL 30 DAY;

-- Alternative
SELECT *
FROM Employee
WHERE hire_date >= DATEADD(DAY, -30, GETDATE());

59. How do you create a view?

sql

CREATE VIEW high_salary_employees AS


SELECT employee_id, name, salary, department
FROM Employee
WHERE salary > 75000;

60. What is the difference between a view and a table?


Table: Physical storage of data

View: Virtual table based on query result, no physical storage

61. Write a query to find the quarter of a given date.

sql

SELECT
date_column,
CASE
WHEN MONTH(date_column) BETWEEN 1 AND 3 THEN 'Q1'
WHEN MONTH(date_column) BETWEEN 4 AND 6 THEN 'Q2'
WHEN MONTH(date_column) BETWEEN 7 AND 9 THEN 'Q3'
ELSE 'Q4'
END as quarter
FROM your_table;

-- Using built-in function


SELECT date_column, QUARTER(date_column) as quarter
FROM your_table;

62. How do you handle duplicate records during INSERT?


sql

-- MySQL
INSERT IGNORE INTO table_name VALUES (...);

-- PostgreSQL
INSERT INTO table_name VALUES (...)
ON CONFLICT (unique_column) DO NOTHING;

-- SQL Server
IF NOT EXISTS (SELECT 1 FROM table_name WHERE condition)
INSERT INTO table_name VALUES (...);

63. What are stored procedures and how do you create them?

sql

-- SQL Server example


CREATE PROCEDURE GetEmployeesByDepartment
@DepartmentName VARCHAR(50)
AS
BEGIN
SELECT * FROM Employee
WHERE department = @DepartmentName;
END;

64. Write a query to find the age of employees.

sql

SELECT
name,
DATEDIFF(YEAR, birth_date, CURRENT_DATE) as age,
TIMESTAMPDIFF(YEAR, birth_date, CURRENT_DATE) as age_mysql
FROM Employee;

65. How do you find records that exist in one table but not in another?

sql

-- Using LEFT JOIN


SELECT t1.*
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id
WHERE t2.id IS NULL;

-- Using NOT EXISTS


SELECT * FROM table1 t1
WHERE NOT EXISTS (
SELECT 1 FROM table2 t2 WHERE t2.id = t1.id
);

-- Using EXCEPT (SQL Server, PostgreSQL)


SELECT * FROM table1
EXCEPT
SELECT * FROM table2;
66. What is the difference between COALESCE and ISNULL?

COALESCE: ANSI standard, accepts multiple parameters


ISNULL: Database-specific, accepts only two parameters

67. Write a query to calculate year-over-year growth.

sql

WITH yearly_sales AS (
SELECT
YEAR(sale_date) as year,
SUM(amount) as total_sales
FROM sales
GROUP BY YEAR(sale_date)
)
SELECT
year,
total_sales,
LAG(total_sales) OVER (ORDER BY year) as prev_year_sales,
(total_sales - LAG(total_sales) OVER (ORDER BY year)) /
LAG(total_sales) OVER (ORDER BY year) * 100 as growth_rate
FROM yearly_sales;

68. How do you create an index?

sql

-- Regular index
CREATE INDEX idx_employee_department ON Employee(department);

-- Unique index
CREATE UNIQUE INDEX idx_employee_email ON Employee(email);

-- Composite index
CREATE INDEX idx_name_dept ON Employee(name, department);

69. What is a trigger and how do you create one?

sql

-- SQL Server example


CREATE TRIGGER trg_employee_audit
ON Employee
AFTER INSERT, UPDATE, DELETE
AS
BEGIN
INSERT INTO audit_log (table_name, action, timestamp)
VALUES ('Employee', 'Modified', GETDATE());
END;

70. Write a query to find the longest string in a column.


sql

SELECT column_name
FROM table_name
WHERE LENGTH(column_name) = (
SELECT MAX(LENGTH(column_name))
FROM table_name
);

71. How do you perform a self-join?

sql

-- Find employees and their managers


SELECT
e1.name as employee,
e2.name as manager
FROM Employee e1
LEFT JOIN Employee e2 ON e1.manager_id = e2.employee_id;

72. What is CROSS JOIN and when would you use it?
CROSS JOIN returns the Cartesian product of two tables. Use it when you need all possible combinations:

sql

SELECT * FROM table1 CROSS JOIN table2;

73. Write a query to find customers who have never placed an order.

sql

SELECT c.*
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;

74. How do you update a table based on another table?

sql

-- SQL Server
UPDATE t1
SET t1.column1 = t2.column1
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id;

-- MySQL
UPDATE table1 t1
JOIN table2 t2 ON t1.id = t2.id
SET t1.column1 = t2.column1;

75. What is a correlated subquery?


A correlated subquery references columns from the outer query:
sql

SELECT * FROM Employee e1


WHERE salary > (
SELECT AVG(salary)
FROM Employee e2
WHERE e2.department = e1.department
);

Advanced Level Questions (76-115)

76. Explain query execution plan and how to analyze it.


A query execution plan shows how the database engine executes a query. Analyze it by:

Looking for table scans vs index seeks

Identifying expensive operations

Checking join algorithms used


Finding missing indexes

77. What are the different types of indexes and when to use them?
Clustered: Physical ordering of data, one per table
Non-clustered: Separate structure pointing to data rows
Unique: Ensures uniqueness

Filtered: Index with WHERE condition


Columnstore: Optimized for analytical workloads

78. Write a query to find the most expensive operation in query plan.

sql

-- This varies by database system


-- SQL Server example using DMVs
SELECT TOP 10
qs.total_elapsed_time / qs.execution_count as avg_elapsed_time,
qs.sql_handle,
st.text
FROM sys.dm_exec_query_stats qs
CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st
ORDER BY avg_elapsed_time DESC;

79. How do you optimize a slow-running query?


1. Analyze execution plan
2. Add appropriate indexes
3. Rewrite subqueries as JOINs

4. Use WHERE clauses to filter early


5. Avoid SELECT *

6. Consider partitioning for large tables

7. Update table statistics

80. What is database deadlock and how do you resolve it?


Deadlock occurs when two transactions block each other. Resolve by:

Accessing tables in consistent order

Keep transactions short


Use appropriate isolation levels

Implement deadlock detection and retry logic

81. Explain different isolation levels in SQL.


READ UNCOMMITTED: Allows dirty reads
READ COMMITTED: Prevents dirty reads

REPEATABLE READ: Prevents dirty and non-repeatable reads


SERIALIZABLE: Highest isolation, prevents all phenomena

82. Write a query using MERGE to implement UPSERT operation.

sql

MERGE target_table AS target


USING (VALUES (1, 'John', 50000)) AS source (id, name, salary)
ON target.id = source.id
WHEN MATCHED THEN
UPDATE SET name = source.name, salary = source.salary
WHEN NOT MATCHED THEN
INSERT (id, name, salary) VALUES (source.id, source.name, source.salary);

83. How do you implement pagination efficiently?

sql

-- OFFSET/FETCH (SQL Server, PostgreSQL)


SELECT * FROM Employee
ORDER BY employee_id
OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY;

-- LIMIT/OFFSET (MySQL, PostgreSQL)


SELECT * FROM Employee
ORDER BY employee_id
LIMIT 10 OFFSET 20;

-- Cursor-based pagination (more efficient for large datasets)


SELECT * FROM Employee
WHERE employee_id > @last_id
ORDER BY employee_id
LIMIT 10;

84. What are table hints and when should you use them?
Table hints force the query optimizer to use specific strategies:

sql

-- SQL Server examples


SELECT * FROM Employee WITH (NOLOCK); -- Read uncommitted
SELECT * FROM Employee WITH (INDEX(idx_name)); -- Force index usage
85. Write a query to find islands and gaps in sequential data.

sql

-- Find islands (consecutive sequences)


WITH grouped_data AS (
SELECT
id,
id - ROW_NUMBER() OVER (ORDER BY id) as group_id
FROM your_table
)
SELECT
MIN(id) as island_start,
MAX(id) as island_end,
COUNT(*) as island_size
FROM grouped_data
GROUP BY group_id
ORDER BY island_start;

86. How do you implement a running balance calculation?

sql

SELECT
transaction_date,
amount,
SUM(amount) OVER (
ORDER BY transaction_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) as running_balance
FROM transactions
ORDER BY transaction_date;

87. What is the difference between UNION and INTERSECT?


UNION: Combines results from multiple queries

INTERSECT: Returns common rows between queries

88. Write a query to find hierarchical data (parent-child relationships).


sql

-- Using recursive CTE


WITH employee_hierarchy AS (
-- Anchor: find top-level employees
SELECT employee_id, name, manager_id, 0 as level,
CAST(name AS VARCHAR(MAX)) as path
FROM Employee
WHERE manager_id IS NULL

UNION ALL

-- Recursive: find subordinates


SELECT e.employee_id, e.name, e.manager_id, eh.level + 1,
eh.path + ' -> ' + e.name
FROM Employee e
JOIN employee_hierarchy eh ON e.manager_id = eh.employee_id
)
SELECT * FROM employee_hierarchy ORDER BY level, path;

89. How do you handle large result sets efficiently?


1. Use appropriate indexing

2. Implement pagination

3. Use streaming/cursor-based processing


4. Consider result set caching

5. Use connection pooling


6. Optimize network round trips

89. What is query plan caching and how does it work?


Query plan caching stores compiled execution plans to avoid recompilation:

Plans are cached based on query text and parameters


Reduces CPU overhead for repeated queries

Can be cleared manually or automatically

90. Write a query to calculate percentiles.

sql

SELECT
department,
PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY salary) as Q1,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) as median,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY salary) as Q3,
PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY salary) as P90
FROM Employee
GROUP BY department;

91. How do you implement custom aggregation functions?


sql

-- Example: Creating a custom aggregate for concatenation


-- This varies significantly by database system

-- SQL Server CLR aggregate example structure


CREATE AGGREGATE string_concat(@input NVARCHAR(MAX), @separator NVARCHAR(10))
RETURNS NVARCHAR(MAX)
EXTERNAL NAME [assembly_name].[class_name];

92. What are materialized views and when should you use them?
Materialized views store query results physically:

Use for expensive aggregations

Good for reporting/analytics


Need refresh strategy

Trade storage for performance

93. Write a query to implement data deduplication logic.

sql

-- Remove duplicates keeping the latest record


WITH ranked_records AS (
SELECT *,
ROW_NUMBER() OVER (
PARTITION BY duplicate_key
ORDER BY created_date DESC
) as rn
FROM your_table
)
DELETE FROM ranked_records WHERE rn > 1;

94. How do you implement slowly changing dimensions (SCD)?

sql

-- Type 2 SCD implementation


MERGE dimension_table AS target
USING staging_table AS source
ON target.business_key = source.business_key
AND target.is_current = 1

WHEN MATCHED AND target.data_hash <> source.data_hash THEN


UPDATE SET is_current = 0, end_date = GETDATE()

WHEN NOT MATCHED THEN


INSERT (business_key, attributes, is_current, start_date, end_date)
VALUES (source.business_key, source.attributes, 1, GETDATE(), NULL);

-- Insert new version for changed records


INSERT INTO dimension_table (business_key, attributes, is_current, start_date)
SELECT s.business_key, s.attributes, 1, GETDATE()
FROM staging_table s
JOIN dimension_table d ON s.business_key = d.business_key
WHERE d.is_current = 0 AND d.end_date = GETDATE();
95. What are database statistics and why are they important?
Database statistics provide information about data distribution:

Help query optimizer choose best execution plans

Include histograms, cardinality estimates


Should be updated regularly

Can be manually updated or auto-updated

96. Write a query to implement time-series analysis.

sql

-- Calculate moving averages and trends


SELECT
date_column,
value,
AVG(value) OVER (
ORDER BY date_column
ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
) as moving_avg_7day,
LAG(value, 12) OVER (ORDER BY date_column) as value_12_periods_ago,
(value - LAG(value, 12) OVER (ORDER BY date_column)) /
LAG(value, 12) OVER (ORDER BY date_column) * 100 as yoy_growth
FROM time_series_table
ORDER BY date_column;

97. How do you implement database partitioning strategies?

sql

-- Range partitioning example (SQL Server)


CREATE PARTITION FUNCTION pf_sales_date (DATE)
AS RANGE RIGHT FOR VALUES
('2022-01-01', '2022-04-01', '2022-07-01', '2022-10-01');

CREATE PARTITION SCHEME ps_sales_date


AS PARTITION pf_sales_date
TO ([PRIMARY], [Q1_2022], [Q2_2022], [Q3_2022], [Q4_2022]);

CREATE TABLE sales (


sale_id INT,
sale_date DATE,
amount DECIMAL(10,2)
) ON ps_sales_date(sale_date);

98. What are query hints and how do you use them effectively?
Query hints override optimizer decisions:
sql

-- Force specific join algorithm


SELECT /*+ USE_HASH(e, d) */ *
FROM employees e JOIN departments d ON e.dept_id = d.dept_id;

-- Force index usage


SELECT * FROM employees /*+ INDEX(emp_idx_name) */
WHERE name = 'John';

99. Write a query to implement data masking for sensitive information.

sql

-- Dynamic data masking


SELECT
employee_id,
LEFT(name, 1) + '***' + RIGHT(name, 1) as masked_name,
LEFT(email, 3) + '***@' + SUBSTRING(email, CHARINDEX('@', email) + 1, LEN(email)) as masked
CASE
WHEN salary < 50000 THEN 'Low'
WHEN salary < 100000 THEN 'Medium'
ELSE 'High'
END as salary_band
FROM employees;

100. How do you implement database monitoring and performance tracking?

sql

-- Example monitoring queries (SQL Server

You might also like