100 SQL Interview Q&A
100 SQL Interview Q&A
100 SQL Interview Q&A
Interview Q&A
Made by Want More
1.What is SQL?
DCL (Data Control Language): Used for controlling access to data in the
database (e.g., GRANT, REVOKE).
INNER JOIN: Returns only the matching rows from both tables.
LEFT JOIN: Returns all rows from the left table and the matching rows
from the right table.
RIGHT JOIN: Returns all rows from the right table and the matching rows
from the left table.
FULL JOIN: Returns all rows from both tables, including the unmatched
rows.
8. What is a subquery?
UNION ALL: It also combines the result sets of two or more SELECT
statements into a single result set but does not remove duplicate rows. It
is faster than UNION as it does not perform duplicate removal.
A view is a virtual table derived from one or more tables or views. It acts as
a stored query and does not store any data itself. Views simplify complex
queries, provide security, and present a customized representation of the
data.
17. What is the difference between a candidate key and a primary key?
20. What is the difference between the HAVING and WHERE clauses in
SQL?
24. What is the difference between UNION and UNION ALL in SQL?
UNION ALL: It also combines the result sets of two or more SELECT
statements into a single result set but does not remove duplicate rows. It
is faster than UNION as it does not perform duplicate removal.
26. What is the difference between a primary key and a unique key?
29. What is the difference between a LEFT JOIN and a RIGHT JOIN?
LEFT JOIN: It returns all rows from the left table and the matching rows
from the right table. If there is no match, NULL values are returned for the
columns of the right table.
RIGHT JOIN: It returns all rows from the right table and the matching rows
from the left table. If there is no match, NULL values are returned for the
columns of the left table.
30. What is a trigger in SQL?
32. What is the difference between the GROUP BY and HAVING clauses?
MINUS: It is used to return the rows from the first query that are not
present in the second query. It removes duplicates from the result set.
EXCEPT: It is used to return the rows from the first query that are not
present in the second query. It preserves duplicates in the result set.
37. What is the difference between the EXISTS and IN operators in SQL?
A recursive query is a query that references its own output in the query
definition. It is used to perform hierarchical or self-referencing queries,
such as traversing tree structures or calculating running totals.
42. What is the purpose of the PIVOT and UNPIVOT operators in SQL?
The WITH clause, also known as a Common Table Expression (CTE), is used
to define temporary result sets that can be referenced within a query. It
simplifies complex queries and improves query readability.
The TOP clause is used to limit the number of rows returned by a query. It
specifies the maximum number of rows to be included in the result set.
48. What is the purpose of the CROSS APPLY and OUTER APPLY
operators in SQL?
50. What is the purpose of the LEAD and LAG functions in SQL?
LEAD: It is used to access the value of a column from the next row in the
result set.
LAG: It is used to access the value of a column from the previous row in
the result set.
The TRUNCATE TABLE statement is used to remove all rows from a table,
effectively deleting all data in the table. Unlike the DELETE statement, it
does not generate individual delete operations for each row and is faster
for large tables.
The RANK() function is used to assign a rank to each row within a result
set based on a specified criteria. It assigns the same rank to rows with the
same values and leaves gaps in the ranking sequence in case of ties.
The NTILE() function is used to divide a result set into a specified number
of groups, assigning a group number to each row. It is often used for data
distribution or bucketing purposes.
The NULLIF function is used to compare two expressions and return null if
they are equal. It is often used to handle division by zero errors or to
replace specific values with null.
57. What is the purpose of the HAVING clause in SQL?
The HAVING clause is used to filter groups of rows after the GROUP BY
operation has been performed. It is used with aggregate functions and
conditions.
The UNION operator is used to combine the result sets of two or more
SELECT statements into a single result set. It removes duplicate rows from
the result set.
The UNION ALL operator is used to combine the result sets of two or more
SELECT statements into a single result set, including duplicate rows. It is
faster than UNION as it does not perform duplicate removal.
The INNER JOIN is used to retrieve records that have matching values in
both tables being joined. It returns only the rows where the join condition
is satisfied.
The LEFT JOIN is used to retrieve all records from the left table and the
matching records from the right table. If there is no match, NULL values
are returned for the columns of the right table.
The RIGHT JOIN is used to retrieve all records from the right table and the
matching records from the left table. If there is no match, NULL values are
returned for the columns of the left table.
The FULL JOIN is used to retrieve all records from both tables being joined,
regardless of whether there is a match or not. If there is no match, NULL
values are returned for the columns of the non-matching table.
The ORDER BY clause is used to sort the result set based on one or more
columns. It specifies the sort order, which can be ascending (ASC) or
descending (DESC).
The DISTINCT keyword is used to eliminate duplicate rows from the result
set. It considers the entire row when determining duplicates.
The COUNT() function is used to count the number of rows that match a
specified condition in a SELECT statement. It can also be used without a
condition to count all rows in a table.
The DATE() function is used to extract the date part from a datetime value.
The YEAR() function is used to extract the year part from a date or
datetime value.
The MONTH() function is used to extract the month part from a date or
datetime value.
84. What is the purpose of the DAY() function in SQL?
The DAY() function is used to extract the day part from a date or datetime
value.
The GETDATE() function is used to retrieve the current date and time.
The NOW() function is used to retrieve the current date and time.
The NULLIF() function is used to compare two expressions and return null
if they are equal. It is often used to handle division by zero errors or to
replace specific values with null.
The TRIM() function is used to remove leading and trailing spaces from a
string.
The CAST() function is used to convert a value of one data type to another
data type.
The ISNULL() function is used to replace null values with a specified value.