What SQL clause is used to filter rows in a result set based on specific conditions?
Explanation:
The WHERE clause is used to specify conditions that rows must meet to be included in the query results.
Which data type would be most suitable for storing a person's last name in a SQL database?
Explanation:
VARCHAR (Variable Character) is used for storing strings of text, making it appropriate for a last name which can vary in length.
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 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.
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.
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.
In a SQL query, what does the logical operator 'AND' do?
Explanation:
The AND operator requires both conditions to be true for the combined condition to be true.
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 statement is used to remove rows from a table?
Explanation:
While both DELETE and TRUNCATE remove rows, DELETE is used to selectively remove rows based on a condition, while TRUNCATE removes all rows.
What does the asterisk (*) symbol represent when used in a SELECT statement?
Explanation:
In SQL, the asterisk (*) is a shorthand to select all columns from a table when used in the SELECT clause.
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.
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 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.
What does SQL stand for?
Explanation:
SQL stands for Structured Query Language. It's a standardized language for managing and interacting with relational databases.