What is the purpose of a subquery in SQL?
Explanation:
A subquery is a query nested inside another query, used to fetch data that the main query then uses, effectively acting as a dynamic input for the main query.
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.
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 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.
What is the function of the SQL 'WHERE' clause?
Explanation:
The WHERE clause in SQL acts as a filter, allowing you to select only those rows that meet the condition you define.
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 is the key difference between the IN and EXISTS operators when used with subqueries?
Explanation:
IN compares a value against a list of values returned by a subquery. EXISTS checks whether the subquery returns any rows at all, regardless of the values.
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 aggregate function would you use to find the highest value in a column?
Explanation:
MAX()
is an aggregate function that returns the maximum value in a set of values (typically a column).
Which type of subquery is used to check if a value exists in a list of values returned by another query?
Explanation:
An IN subquery checks if a specific value exists within the set of results returned by the subquery, making it useful for checking against multiple possibilities.
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.
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.
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.
What is the purpose of the 'OR' operator in a SQL WHERE clause?
Explanation:
The OR operator allows rows to be included if they satisfy either one or both of the connected conditions.
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.
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.
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 is the primary clause used to retrieve data from a table in SQL?
Explanation:
The SELECT clause is the core of retrieving data in SQL. It specifies the columns you want to get from a table.
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 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.