What is the purpose of the COUNT()
function in SQL?
Explanation:
COUNT()
is an aggregate function used to count the number of rows in a table or the number of rows that meet a specific condition in a query.
How would you sort results in descending order by the 'date_created' column?
Explanation:
The 'DESC' keyword after the column name in the ORDER BY clause signals descending order sorting.
What is the purpose of the 'LIMIT' clause in SQL?
Explanation:
LIMIT is used to control how many rows are included in the query result, helping manage the amount of data returned.
What potential issue should be considered when using correlated subqueries?
Explanation:
Correlated subqueries execute for each row of the outer query, potentially leading to performance bottlenecks, especially with large datasets. Proper indexing and optimization are crucial.
What does SQL stand for?
Explanation:
SQL stands for Structured Query Language. It's a standardized language for managing and interacting with relational databases.
Which comparison operator means 'not equal to' in SQL?
Explanation:
Both '!=' and '<>' are used for 'not equal to' in SQL. They are interchangeable.
Which SQL clause is used to sort the result set?
Explanation:
ORDER BY is the clause used in SQL to sort the result set based on a specified column.
Which SQL statement is used to add new rows into a table?
Explanation:
The INSERT statement is used to insert new rows with data into an existing table within a database.
By default, how does the 'ORDER BY' clause sort data?
Explanation:
If no specific order (ASC or DESC) is specified, ORDER BY defaults to sorting in ascending order.
What is the difference between CURDATE()
and NOW()
in SQL?
Explanation:
CURDATE()
returns only the current date in 'YYYY-MM-DD' format, while NOW()
returns both the current date and time.
Which SQL function would you use to combine the first and last names from two different columns into a single column?
Explanation:
CONCAT()
is a string function that takes two or more strings as arguments and concatenates them into a single string.
What is the result of SELECT NOW();
in SQL?
Explanation:
NOW()
is a date function that returns the current date and time in the format 'YYYY-MM-DD HH:MM:SS'.
How would you convert the text 'hello world' to uppercase in SQL?
Explanation:
UPPER()
is a string function that takes a string as an argument and returns the string converted to uppercase.
What type of join returns all rows from the left table, even if there are no matching rows in the right table?
Explanation:
A LEFT JOIN
returns all rows from the left table (the table mentioned before LEFT JOIN
) and the matching rows from the right table. If there are no matches, it fills in NULL
values for the columns from the right table.
You want to find customers who have placed orders in the last month. You have two tables: 'Customers' and 'Orders'. Which subquery type would be most efficient to achieve this?
Explanation:
EXISTS subqueries are generally more efficient when checking for the existence of rows, especially in larger datasets, as it stops evaluation once a single matching row is found.
In which scenario would a subquery be a more suitable choice than a JOIN operation?
Explanation:
Subqueries excel at providing dynamic filtering conditions for the main query. If the filtering relies on a separate query's results, using a subquery makes the logic clearer and more concise than attempting to achieve the same with JOINs.
What SQL clause is used to group rows with the same value in one or more columns into a summary row?
Explanation:
The GROUP BY clause is specifically designed to group rows based on identical values in specified columns, allowing for aggregate functions to be applied to each group.
Which keyword is used in conjunction with the UPDATE statement to specify the conditions for updating rows?
Explanation:
The WHERE clause is crucial with UPDATE as it defines which rows should be modified. Without it, all rows would be updated.
You are using a subquery to fetch a list of product IDs from an 'Orders' table. What is the main purpose of using the DISTINCT keyword within this subquery?
Explanation:
The DISTINCT keyword ensures that the subquery's result set contains only unique values, removing any duplicate product IDs, which is essential for accurate comparisons or joins in the main query.
You want to select the department with the highest average salary. What SQL clause would you use to filter the grouped results based on the calculated average salary?
Explanation:
HAVING is used to filter groups after the GROUP BY clause, making it suitable for conditions involving aggregate functions like AVG used for calculating the average salary.