Practice writing SQL queries using SELECT, WHERE, and JOIN to retrieve, filter, and combine data from relational tables.
Read each problem carefully. Write the SQL query or explain the result. Use clear table and column names from the problem.
Querying tables, filtering rows, and combining data
CS - Grade 9-12
- 1
A table named Students has columns student_id, first_name, last_name, and grade_level. Write a SQL query that shows only the first_name and last_name columns for every student.
- 2
A table named Books has columns book_id, title, author, and pages. Write a SQL query that shows all columns for books with more than 300 pages.
- 3
A table named Movies has columns movie_id, title, rating, and release_year. Write a SQL query that shows the title and release_year of movies with a rating equal to 'PG-13'.
- 4
Use this table: Employees(employee_id, first_name, last_name, department, salary). Write a SQL query that shows all employees in the 'Sales' department who have a salary greater than 50000.
- 5
A table named Products has columns product_id, product_name, category, and price. Write a SQL query that shows product_name and price for products in the 'Electronics' category with a price less than or equal to 100.
- 6
Two tables are shown. Customers has columns customer_id, name, and city. Orders has columns order_id, customer_id, and order_date. Write a SQL query that shows each order_id with the customer's name.
- 7
Two tables are shown. Authors has columns author_id and author_name. Books has columns book_id, title, and author_id. Write a SQL query that shows each book title with its author_name.
- 8
Use these tables: Students(student_id, name) and Enrollments(enrollment_id, student_id, course_name). Write a SQL query that shows the names of students enrolled in 'Biology'.
- 9
Use these tables: Teams(team_id, team_name) and Players(player_id, player_name, team_id). Write a SQL query that shows player_name and team_name for every player.
- 10
A table named Sales has columns sale_id, item_name, quantity, and store_location. Write a SQL query that shows item_name and quantity for sales where store_location is 'Denver' or 'Austin'.
- 11
Use these tables: Courses(course_id, course_name, teacher_id) and Teachers(teacher_id, teacher_name). Write a SQL query that shows course_name and teacher_name only for courses taught by 'Ms. Rivera'.
- 12
Explain the difference between SELECT, WHERE, and JOIN in SQL. Use one sentence for each keyword.