Consider a table Employees with columns id, name, and department. The table has these rows:
- (1, 'Alice', 'HR')
- (2, 'Bob', 'IT')
- (3, 'Charlie', 'IT')
What is the result of this query?
SELECT department, COUNT(*) AS count FROM Employees GROUP BY department;
SELECT department, COUNT(*) AS count FROM Employees GROUP BY department;
Grouping counts how many rows share the same department.
The query groups rows by department and counts how many employees are in each. HR has 1 employee, IT has 2.
In the relational model, what is the main purpose of a primary key in a table?
Think about how you find one specific record in a table.
A primary key ensures each row is unique and can be identified without confusion.
Given a table Employees with a nullable department column, which query correctly returns employees where department is not assigned?
Remember how SQL treats NULL comparisons.
In SQL, IS NULL checks for NULL values. Using = NULL or != NULL does not work as expected.
Consider a large Orders table with columns order_id, customer_id, and order_date. You run this query often:
SELECT * FROM Orders WHERE customer_id = 12345;
Which index will improve query speed the most?
Think about which column is used in the WHERE clause.
Indexing the column used in the WHERE clause speeds up filtering. Here, customer_id is filtered.
Given tables Students(id, name) and Enrollments(student_id, course_id), this query returns no rows:
SELECT s.name FROM Students s JOIN Enrollments e ON s.id = e.student_id WHERE e.course_id = 101;
What is the most likely reason?
Check if any rows match the filter condition.
If no enrollments exist for course 101, the JOIN returns no rows. The JOIN condition is correct.