0% found this document useful (0 votes)
8 views6 pages

SQL 3

The document outlines key SQL concepts including the LIMIT clause for restricting query results, the DISTINCT keyword for eliminating duplicates, and the ORDER BY clause for sorting results. It also explains the GROUP BY clause for aggregating data, along with set operators like UNION and INTERSECT for combining result sets. Additionally, it covers logical operators such as AND, OR, and NOT, as well as aggregate functions like COUNT, SUM, AVG, MIN, and MAX.
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)
8 views6 pages

SQL 3

The document outlines key SQL concepts including the LIMIT clause for restricting query results, the DISTINCT keyword for eliminating duplicates, and the ORDER BY clause for sorting results. It also explains the GROUP BY clause for aggregating data, along with set operators like UNION and INTERSECT for combining result sets. Additionally, it covers logical operators such as AND, OR, and NOT, as well as aggregate functions like COUNT, SUM, AVG, MIN, and MAX.
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/ 6

Interview Theory Questions

1. Explain the purpose of the LIMIT clause in SQL.

The LIMIT clause is used to restrict the number of rows returned by a query, effectively
limiting the result set.

2.How would you implement pagination using the LIMIT clause?

Pagination is achieved by combining the LIMIT clause with an OFFSET or using the
OFFSET shorthand. For example, LIMIT 10 OFFSET 20 retrieves rows 21 to 30.

3. When might you use the LIMIT clause in a SELECT query?

The LIMIT clause is often used when you want to view a subset of rows, implement
pagination, or limit the size of the result set for performance reasons.

4 Explain the purpose of the DISTINCT keyword in SQL.

DISTINCT is used to eliminate duplicate rows from the result set, ensuring that only
unique values are included.

5. How is the DISTINCT keyword different from the GROUP BY clause?

DISTINCT is used to retrieve unique values from one or more columns, while GROUP
BY is used for aggregations, grouping rows based on specified columns.

6.Can you use DISTINCT with multiple columns?

Yes, you can use DISTINCT with multiple columns to retrieve unique combinations of
values across those columns.

7.Is there a specific "not distinct" keyword or concept in SQL?

No, there is no specific "not distinct" keyword. If you don't use the DISTINCT keyword,
the query naturally includes all rows, including duplicates.
8. How would you retrieve all rows from a column, including duplicate values?

Simply omit the DISTINCT keyword from the SELECT query. For example, SELECT
column_name FROM table_name;

9 In what scenarios might you intentionally include duplicate values in a result


set?

You might include duplicates when you need to view the raw, unaltered data or when
aggregating functions require multiple occurrences of the same value.

1. What is the purpose of the ORDER BY clause in SQL?

The ORDER BY clause is used to sort the result set of a query based on one or more
columns in ascending or descending order.

2. Can you use multiple columns in the ORDER BY clause? If yes, how do you
specify the order for each column?

Yes, you can use multiple columns in the ORDER BY clause. To specify the order for
each column, you list them separated by commas. For example: ORDER BY column1
ASC, column2 DESC.

3. Explain the difference between ORDER BY and GROUP BY.

ORDER BY is used to sort the result set, while GROUP BY is used to group rows that
have the same values in specified columns into summary rows.

4. What is the default sort order in the ORDER BY clause if you don't specify ASC
or DESC?

The default sort order is ascending (ASC) if you don't specify ASC or DESC.

5. Can you use column aliases in the ORDER BY clause?

No, you cannot use column aliases in the ORDER BY clause. You must use the original
column name or column expression.

6. What is the purpose of the GROUP BY clause in SQL?


The GROUP BY clause is used to group rows that have the same values in specified
columns into summary rows, like using aggregate functions.

7. Can you use aggregate functions without using the GROUP BY clause?

Yes, you can use aggregate functions without the GROUP BY clause, but they will
operate on the entire result set, not on grouped subsets.

8. Explain the HAVING clause in conjunction with the GROUP BY clause.

The HAVING clause is used in conjunction with GROUP BY to filter the results of a
grouped query. It is similar to the WHERE clause but is applied after the grouping.

9. What is the difference between WHERE and HAVING when used with the
GROUP BY clause?

The WHERE clause filters rows before grouping, while the HAVING clause filters
grouped rows after the grouping.

10. Can you use the GROUP BY clause without using any aggregate functions?

No, when using GROUP BY, every selected column must be either an aggregate
function or included in the GROUP BY clause.

11. What is the purpose of the UNION operator in SQL?

The UNION operator is used to combine the result sets of two or more SELECT
statements. It removes duplicate rows from the combined result set.

12. Explain the key characteristics of the UNION operator.

The number and order of columns must be the same in all SELECT statements.
Column data types must be compatible.
The result set is sorted and duplicates are removed by default.
Use UNION ALL if you want to include duplicate rows.

13. How does the INTERSECT operator work in SQL?

The INTERSECT operator returns the common rows between the result sets of two
SELECT statements. It is used to find the intersection of sets, meaning it retrieves rows
that appear in both result sets.
14. Can you use the INTERSECT operator with multiple SELECT statements?

In standard SQL, the INTERSECT operator is binary and can be used with only two
SELECT statements. However, some database systems may support additional syntax
for more than two sets.

15. Explain the common use case for the SET operators (UNION, INTERSECT, and
EXCEPT).

SET operators are useful when dealing with different tables or conditions where you
want to perform operations similar to set operations, such as combining, finding
intersections, or finding differences between sets of data.

16. Can you use the WHERE clause with the UNION operator?

Yes, you can use the WHERE clause with each SELECT statement within the UNION to
filter the rows before combining them.

17. How does the EXCEPT operator differ from the INTERSECT operator?

The EXCEPT operator returns the rows that are unique to the first SELECT statement
but not present in the second SELECT statement. In contrast, the INTERSECT operator
returns common rows between two SELECT statements.
1.Explain the difference between the AND and OR operators in SQL.

The AND operator is used to combine two or more conditions, and all conditions must
be true for a row to be included in the result set. The OR operator, on the other hand,
retrieves rows if at least one of the conditions is true.

2. How do you retrieve records where either Condition A or Condition B is true in


a SQL query?

You can use the OR operator to retrieve records where either Condition A or Condition
B is true.

3. Explain the purpose of the NOT operator in SQL.

The NOT operator is used to negate a condition, meaning it retrieves records that do
not satisfy the specified condition.

4.What is the purpose of the COUNT() function in SQL?

The COUNT() function is used to count the number of rows in a table that meet a
specified condition.

5.How does the SUM() function work in SQL, and provide an example.

The SUM() function calculates the sum of values in a numeric column. Example:
SELECT SUM(Salary) FROM employees;

6.Explain the difference between the AVG() and SUM() functions.

The AVG() function calculates the average value of a numeric column, while the SUM()
function calculates the total sum of values in the column.

7.How is the MIN() function used in SQL?

The MIN() function retrieves the minimum value from a numeric column in a table.

8.Provide an example of using the MAX() function in a SQL query.


SELECT MAX(Price) FROM products; retrieves the maximum price from the "products"
table.

9.How can you handle cases where there are NULL values in aggregate
functions?

Aggregate functions ignore NULL values by default. If you want to include NULL values
in the calculation, you can use the IFNULL() or COALESCE() function to replace NULL
with a default value.

10.Explain the purpose of the GROUP BY clause in conjunction with aggregate


functions.

The GROUP BY clause is used to group rows that have the same values in specified
columns, allowing aggregate functions to perform calculations on each group
separately.

You might also like