Test your knowledge with these 15 medium-difficulty SQL interview questions, covering SQL basics, key concepts, commands, and common scenarios designed to help you succeed in SQL-related interviews.
Which statement best describes the difference between SQL and a database management system such as MySQL?
Explanation: SQL is the language used to query and manage relational databases, whereas MySQL is a database management system that uses SQL. SQL is not hardware, nor is it a company-owned product (the second and fourth options are incorrect). While SQL is for querying, MySQL also handles data storage and administration, so the third option is inaccurate.
In which way does PL/SQL differ from standard SQL when used in a database?
Explanation: PL/SQL adds procedural features like loops and conditional statements on top of regular SQL, allowing for more complex logic. PL/SQL does not replace all DML commands or is not focused solely on data visualization, so the second and third choices are wrong. It is not limited to open-source databases, making the last option incorrect.
Which SQL command is classified as Data Definition Language (DDL) and is used to permanently remove a table from a database?
Explanation: The DROP command is a DDL command that removes an object like a table from the database permanently. DELETE is a DML command that removes rows but does not affect the structure. UPDATE modifies data, and MERGE is used for conditional insert or update, so these do not define or remove the table structure.
What is the main function of the BETWEEN keyword in an SQL WHERE clause?
Explanation: BETWEEN checks if a value is between two specified limits, including both endpoints. It doesn't combine columns (second option), perform table joins (third option), or remove duplicates (fourth option).
How is the SQL IN keyword most commonly used when filtering query results?
Explanation: The IN keyword is used to determine if a value exists within a specified list or the set returned by a subquery. It does not insert or update records (second and fourth options), nor is it used for deduplication (third option).
What is an SQL view, and one its primary uses?
Explanation: A view is essentially a stored SELECT statement that can be queried like a table and used for abstraction or security. It is not an index or a trigger (second and third options), nor does it store data permanently like a table (last option).
What is a key difference between a standard SQL view and a materialized view?
Explanation: A normal view stores only the SQL query definition, so it always runs live; a materialized view actually stores query results for faster access. Materialized views can be refreshed; they do not necessarily include triggers, and both types may use indexes depending on the system, so the other options are incorrect.
Which statement correctly defines a primary key in SQL?
Explanation: The primary key must be unique for every row and not null, ensuring row identity. It does not allow duplicates or nulls (second option incorrect), is not required for every table (third option), and is not just for sorting (fourth option).
How does a foreign key enforce relationships between two tables?
Explanation: A foreign key references a primary key in another table, enforcing data validity and relationships. It doesn't merely index (second option), is not a backup (third option), and doesn't force row counts to match (last option).
In a relational database, what best defines a composite key?
Explanation: Composite keys consist of multiple columns to guarantee each row is unique when one column alone is insufficient. They do not refer to multiple tables (second option), nor are they virtual or simply duplicated from another table (third and fourth options).
What is a surrogate key in a database table?
Explanation: Surrogate keys are generic unique identifiers, usually numbers, created specifically to serve as primary keys. They do not store business-meaningful codes (second option), are not natural key combinations (third option), and do not always track row changes (fourth option).
Which of the following statements is an example of a Data Manipulation Language (DML) command?
Explanation: UPDATE modifies records and belongs to DML commands. CREATE, ALTER, and DROP are DDL commands used for defining or altering the database structure, so they are not considered DML.
Which DDL command allows you to add a description or comment to the database data dictionary?
Explanation: The COMMENT statement adds metadata to the data dictionary, typically for documentation. CHECK is a constraint, REMARK is not a valid SQL command, and RENAME is for changing object names.
What is the main function of the MERGE statement in SQL?
Explanation: MERGE is used to insert rows or update them if they already exist, handling both in one command. It does not combine tables into one object (second option), is not only for updates (third option), and is unrelated to JOIN operations (fourth option).
If you issue a TRUNCATE TABLE employees command, what happens to the data?
Explanation: TRUNCATE removes all rows but keeps the table and its structure in the database. It is different from DROP (which removes the entire table), doesn't just delete one row, and does not target indexes specifically.
What does the EXPLAIN PLAN SQL command show you?
Explanation: EXPLAIN PLAN reveals how the database intends to execute a query, helping with optimization. It does not return unique values (second option), grant privileges (third option), or interact with procedures (fourth option).