SQL Database Course
- 52Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the most general definition of a database?
52 questions
- 1
What is the most general definition of a database?
Model answer
A database is any collection of related information.
Explanation
This broad definition encompasses everything from a simple shopping list to a complex system like Amazon's userbase.
- 2
What is a database management system (DBMS)?
Model answer
A DBMS is a software program that helps users create and maintain a database on a computer.
Explanation
It handles storage, security, backup, and interaction with applications, making it easier to manage large amounts of data.
- 3
What does the acronym CRUD stand for?
Model answer
Create, Read, Update, and Delete (sometimes Retrieve).
Explanation
These are the four basic operations performed on data in a database.
- 4
What is the main difference between relational and non-relational databases?
Model answer
Relational databases organize data into tables with rows and columns, while non-relational databases use other structures like documents, graphs, or key-value pairs.
Explanation
Relational databases use SQL and are table-based; non-relational (NoSQL) databases are flexible and do not require a fixed schema.
- 5
What is a database query?
Model answer
A request made to the database management system for specific information.
Explanation
Queries are written in a language like SQL and allow users to retrieve precise data from complex schemas.
- 6
Define a primary key in a relational database table.
Model answer
A primary key is an attribute that uniquely identifies each row in the table. It must be unique and not null.
Explanation
It ensures that no two rows have the same value and is essential for referencing rows through foreign keys.
- 7
What is the difference between a surrogate key and a natural key?
Model answer
A surrogate key has no real-world meaning (e.g., an auto-incremented ID), while a natural key has a real-world mapping (e.g., Social Security Number).
Explanation
Surrogate keys are artificial; natural keys are existing identifiers from the real world.
- 8
What is a foreign key?
Model answer
A foreign key is an attribute in one table that stores the primary key of another table, creating a relationship between the tables.
Explanation
It allows linking rows across tables, e.g., an employee's branch_id referring to the branch table's id.
- 9
What is a composite key?
Model answer
A composite key is a primary key made up of two or more columns that together uniquely identify each row.
Explanation
It is used when a single column is not sufficient; for example, branch_id and supplier_name together uniquely identify a branch supplier.
- 10
What are the four types of languages that SQL comprises?
Model answer
SQL is a data query language (DQL), data definition language (DDL), data control language (DCL), and data manipulation language (DML).
Explanation
SQL covers querying, schema definition, user permissions, and inserting/updating/deleting data.
- 11
What is the purpose of the CREATE TABLE statement?
Model answer
It creates a new table in the database with specified columns and their data types.
Explanation
Example: CREATE TABLE student (student_id INT PRIMARY KEY, name VARCHAR(20));
- 12
What does the DESCRIBE command do?
Model answer
It displays the structure of a table, including column names, data types, and key information.
Explanation
Useful for verifying table creation and schema.
- 13
How do you add a new column to an existing table?
Model answer
Use ALTER TABLE table_name ADD column_name datatype;
Explanation
Example: ALTER TABLE student ADD GPA DECIMAL(3,2);
- 14
What is the difference between DROP TABLE and DELETE FROM?
Model answer
DROP TABLE removes the entire table structure and data; DELETE FROM removes rows but keeps the table.
Explanation
DROP is DDL, DELETE is DML.
- 15
How do you insert a row into a table without specifying values for all columns?
Model answer
List the specific column names in the INSERT INTO clause and provide corresponding values; omitted columns get NULL or default values.
Explanation
Example: INSERT INTO student (student_id, name) VALUES (3, 'Claire');
- 16
What does the NOT NULL constraint do?
Model answer
It ensures that a column cannot have a NULL value.
Explanation
Used to enforce that a value is always provided, e.g., a student's name must not be NULL.
- 17
What does the UNIQUE constraint do?
Model answer
It ensures that all values in a column are different from each other.
Explanation
Prevents duplicate entries in that column, e.g., two students cannot have the same email if it's UNIQUE.
- 18
What is the purpose of AUTO_INCREMENT?
Model answer
It automatically generates a unique number for each new row, often used for primary keys.
Explanation
Eliminates the need to manually assign IDs; e.g., student_id INT AUTO_INCREMENT PRIMARY KEY.
- 19
What does the DEFAULT constraint do?
Model answer
It sets a default value for a column when no value is provided during insertion.
Explanation
Example: major VARCHAR(20) DEFAULT 'undecided'.
- 20
How do you update a row's value based on a condition?
Model answer
Use UPDATE table_name SET column = value WHERE condition;
Explanation
Example: UPDATE student SET major = 'Bio' WHERE major = 'Biology';
- 21
What happens if you omit the WHERE clause in an UPDATE statement?
Model answer
All rows in the table will be updated.
Explanation
Be cautious: without WHERE, the change applies to every row.
- 22
How do you delete a specific row from a table?
Model answer
Use DELETE FROM table_name WHERE condition;
Explanation
Example: DELETE FROM student WHERE student_id = 5;
- 23
What is the purpose of the SELECT statement?
Model answer
It retrieves data from one or more tables.
Explanation
The most common SQL command used for querying.
- 24
How do you return only distinct values from a column?
Model answer
Use SELECT DISTINCT column_name FROM table_name;
Explanation
Example: SELECT DISTINCT sex FROM employee; returns only 'M' and 'F'.
- 25
How do you order query results?
Model answer
Use ORDER BY column_name [ASC|DESC];
Explanation
ASC is ascending (default), DESC descending.
- 26
How do you limit the number of rows returned?
Model answer
Use the LIMIT keyword: SELECT * FROM table LIMIT number;
Explanation
Example: SELECT * FROM employee LIMIT 5; returns only the first five employees.
- 27
What does the AS keyword do in a SELECT statement?
Model answer
It renames a column or expression in the result set.
Explanation
Example: SELECT first_name AS forename, last_name AS surname; changes the column headings.
- 28
What are the comparison operators commonly used in WHERE clauses?
Model answer
=, <>, <, >, <=, >=, AND, OR.
Explanation
These allow filtering rows based on conditions.
- 29
What does the IN operator do?
Model answer
It checks if a value matches any value in a list.
Explanation
Example: WHERE name IN ('Claire', 'Kate', 'Mike');
- 30
How do you count the number of rows in a table?
Model answer
SELECT COUNT(*) FROM table_name; or SELECT COUNT(column) FROM table_name;
Explanation
COUNT(*) counts all rows; COUNT(column) counts non-NULL values in that column.
- 31
How do you calculate the average of a numeric column?
Model answer
SELECT AVG(column) FROM table_name;
Explanation
Example: SELECT AVG(salary) FROM employee; gives the average salary.
- 32
How do you sum all values in a numeric column?
Model answer
SELECT SUM(column) FROM table_name;
Explanation
Example: SELECT SUM(salary) FROM employee; returns total payroll.
- 33
What is the purpose of GROUP BY in aggregate queries?
Model answer
It groups rows that have the same values in specified columns, allowing aggregate functions to be applied per group.
Explanation
Example: SELECT COUNT(*), sex FROM employee GROUP BY sex; shows count per gender.
- 34
What wildcard characters are used with the LIKE operator?
Model answer
% (percent) matches any number of characters, and _ (underscore) matches exactly one character.
Explanation
Example: WHERE client_name LIKE '%LLC' finds names ending with 'LLC'.
- 35
What is the UNION operator used for?
Model answer
It combines the result sets of two or more SELECT statements into a single result set.
Explanation
Rules: each SELECT must have the same number of columns and compatible data types.
- 36
What is the difference between INNER JOIN and LEFT JOIN?
Model answer
INNER JOIN returns only rows with matching values in both tables; LEFT JOIN returns all rows from the left table, even if there is no match in the right table.
Explanation
INNER JOIN requires the join condition to be true; LEFT JOIN includes left table rows with NULLs for unmatched right columns.
- 37
How do you write a nested query to find employees who sold over $30,000 to a single client?
Model answer
SELECT first_name, last_name FROM employee WHERE emp_id IN (SELECT emp_id FROM works_with WHERE total_sales > 30000);
Explanation
The inner query gets the employee IDs; the outer query retrieves the names of those employees.
- 38
What does ON DELETE SET NULL do?
Model answer
When a referenced row is deleted, the foreign key columns in referencing rows are set to NULL.
Explanation
Used when the foreign key is not part of the primary key and it's acceptable to lose the reference.
- 39
When must ON DELETE CASCADE be used instead of SET NULL?
Model answer
When the foreign key is part of the primary key (composite key) of the referencing table, because primary key columns cannot be NULL.
Explanation
CASCADE deletes the entire referencing row when the referenced row is deleted.
- 40
What is a trigger in SQL?
Model answer
A block of SQL code that automatically executes when a specified database operation (INSERT, UPDATE, DELETE) occurs on a table.
Explanation
Triggers can be set to run BEFORE or AFTER the operation and can access the new or old row data.
- 41
Why is the DELIMITER changed when creating a trigger in MySQL?
Model answer
Because the trigger body contains semicolons, changing the delimiter (e.g., to $$) allows the entire trigger definition to be sent as a single statement.
Explanation
Without changing the delimiter, MySQL would interpret the semicolons inside the trigger as ending the CREATE statement prematurely.
- 42
What is an entity in an ER diagram?
Model answer
An object or concept that we want to model and store information about, represented by a rectangle.
Explanation
Examples: Student, Class, Employee.
- 43
How is a primary key shown in an ER diagram?
Model answer
By underlining the attribute name inside the oval.
Explanation
It indicates the attribute that uniquely identifies each instance of the entity.
- 44
What is a composite attribute?
Model answer
An attribute that can be broken down into smaller sub-attributes, e.g., Name into First Name and Last Name.
Explanation
In an ER diagram, it is shown as an oval connected to sub-ovals.
- 45
What is a multi-valued attribute?
Model answer
An attribute that can have multiple values for a single entity instance, e.g., clubs a student belongs to.
Explanation
Represented by a double oval in an ER diagram.
- 46
What is a derived attribute?
Model answer
An attribute that can be calculated or derived from other attributes, e.g., age from birth_date.
Explanation
Shown as a dashed oval; it is not stored in the database but is computed.
- 47
What does total participation mean in a relationship?
Model answer
Every instance of the entity must participate in the relationship (shown by a double line).
Explanation
Example: All classes must be taken by at least one student (if total on class side).
- 48
What is relationship cardinality?
Model answer
The number of instances of one entity that can be associated with each instance of another entity in a relationship.
Explanation
Common types: 1:1, 1:N, M:N.
- 49
What is a weak entity type?
Model answer
An entity that cannot be uniquely identified by its own attributes alone and depends on another entity (its owner).
Explanation
Shown with a double rectangle; its primary key combines a partial key with the owner's primary key.
- 50
When mapping a 1:1 binary relationship, where should the foreign key be placed?
Model answer
In the entity that has total participation in the relationship.
Explanation
If both have total or partial, choose one. This minimizes NULL values.
- 51
When mapping a 1:N binary relationship, where is the foreign key added?
Model answer
On the N-side (the many side) of the relationship.
Explanation
Example: Branch (1) to Employee (N): add branch_id as foreign key in Employee table.
- 52
How is an M:N relationship mapped to a relational schema?
Model answer
Create a new table with a composite primary key consisting of the primary keys of both participating entities, plus any relationship attributes.
Explanation
This table (e.g., Works_With) stores the many-to-many association with foreign keys referencing each entity.

Ask anything
We've read this whole study set, so questions about it get the best answers.