Databases & SQL cheat sheet - grade 10-12

Click image to open full size

Databases store organized information so programs can search, update, and analyze data efficiently. This cheat sheet covers relational database structure, SQL commands, keys, joins, and basic database design. Students need these ideas to build apps, interpret data, and understand how websites and information systems manage records. It is designed as a quick reference for reading and writing common SQL statements. The core idea is that related data is stored in tables made of rows and columns. SQL lets you create tables, insert records, filter results, combine tables, group data, and change stored values. Primary keys identify rows, foreign keys connect tables, and normalization helps reduce repeated data. Good queries use clear conditions, correct join logic, and safe update or delete filters.

Key Facts

  • A relational table stores data in rows and columns, where each row is a record and each column is a field or attribute.
  • A primary key uniquely identifies each row in a table, such as student_id in a Students table.
  • A foreign key is a column that references a primary key in another table, such as course_id in an Enrollments table referencing Courses(course_id).
  • The basic query pattern is SELECT columns FROM table WHERE condition ORDER BY column;
  • CRUD means CREATE, READ, UPDATE, and DELETE, which match INSERT, SELECT, UPDATE, and DELETE operations in SQL.
  • An INNER JOIN returns only rows where the join condition matches in both tables, using syntax such as FROM A INNER JOIN B ON A.id = B.a_id.
  • Aggregate functions summarize groups of rows, including COUNT(*), SUM(column), AVG(column), MIN(column), and MAX(column).
  • A safe UPDATE or DELETE statement should usually include WHERE condition, because without WHERE it affects every row in the table.

Vocabulary

Database
A database is an organized collection of data that can be stored, searched, updated, and managed.
Table
A table is a database structure made of rows and columns that stores one type of related data.
Primary Key
A primary key is a column or set of columns that uniquely identifies each row in a table.
Foreign Key
A foreign key is a column that links one table to the primary key of another table.
Query
A query is an instruction written to retrieve, add, change, or delete data in a database.
Normalization
Normalization is the process of organizing tables to reduce repeated data and improve data consistency.

Common Mistakes to Avoid

  • Forgetting the WHERE clause in UPDATE or DELETE is dangerous because it changes or removes every row in the table.
  • Joining tables on the wrong columns gives incorrect matches because the relationship must use matching key columns such as Student.student_id = Enrollment.student_id.
  • Using SELECT * in final queries can return unnecessary data because it fetches every column instead of only the columns needed.
  • Confusing WHERE and HAVING causes errors because WHERE filters individual rows before grouping, while HAVING filters grouped results after GROUP BY.
  • Storing repeated data in one table creates update problems because the same fact may need to be changed in many places.

Practice Questions

  1. 1 A Students table has 120 rows and a Courses table has 8 rows. How many rows would SELECT * FROM Students CROSS JOIN Courses; return?
  2. 2 Write a SQL query to select first_name, last_name, and grade from Students where grade is greater than or equal to 90, sorted from highest grade to lowest grade.
  3. 3 A table Orders has columns order_id, customer_id, order_total, and order_date. Write a query that returns the total money spent by each customer using GROUP BY.
  4. 4 Explain why a school database should store student information and course information in separate tables connected by an Enrollments table instead of storing every course name directly inside the Students table.