Interactive SQL Practicum: 10-Task Practicum

Practice DDL, default-aware INSERTs, analytical SELECT queries, and maintenance operations against the Library schema. Solve each task using the side-by-side ER models and conceptual hints.

⏱ 00:00
Completed: 0 / 10

Schema Model • Books Entity

erDiagram Books { int id PK varchar_150 title "NOT NULL" varchar_20 isbn "UNIQUE NOT NULL" int publicationYear "CHECK > 1800" int totalCopies "DEFAULT 1" }
Task 1: DDL Pending

Create the Books Table

Write a complete CREATE TABLE Books (...) statement conforming strictly to the ER diagram:
  • id: INT Primary Key.
  • title: VARCHAR(150), prohibited from being NULL.
  • isbn: VARCHAR(20), Unique and prohibited from being NULL.
  • publicationYear: INT with a check constraint enforcing values strictly greater than 1800.
  • totalCopies: INT with a default fallback of 1.
Need a Hint?
Structuring table column specifications:
  • To define the primary identifier: id INT PRIMARY KEY.
  • Enforce mandatory textual values with NOT NULL.
  • Enforce unique entries with UNIQUE.
  • Add inline boundary conditions with CHECK (column_name > value).
  • Specify automatic baseline values with DEFAULT value.
  • Declare columns in the same order as the diagram, using real SQL types such as VARCHAR(150) (the diagram's varchar_150 is only notation).

Schema Model • Books Entity

erDiagram Books { int id PK varchar_150 title "NOT NULL" varchar_20 isbn "UNIQUE NOT NULL" int publicationYear "CHECK > 1800" int totalCopies "DEFAULT 1" }
Task 2: DML Insert Pending

Targeted Insert with Defaults

Write a single INSERT INTO Books statement to insert a record:
  • Target specific columns: id = 101, title = 'Database Fundamentals', and isbn = '978-0133970777'.
  • Do not supply values for totalCopies (so it inherits its default 1) or publicationYear (leaving it unpopulated as NULL).
Need a Hint?
Omitting columns with default values:
  • Specify the target column list directly after the table name: INSERT INTO Books (col1, col2, col3) VALUES (...).
  • An omitted column receives its declared DEFAULT; with no default it becomes NULL, but only if the column allows NULL (omitting a NOT NULL column without a default makes the insert fail).

Schema Model • Books Entity

erDiagram Books { int id PK varchar_150 title "NOT NULL" varchar_20 isbn "UNIQUE NOT NULL" int publicationYear "CHECK > 1800" int totalCopies "DEFAULT 1" }
Task 3: SELECT Filter Pending

Filter with Pattern Matching (LIKE)

Write a SELECT query on the Books table:
  • Select only the title and isbn columns.
  • Filter for books whose title starts with the prefix 'Data' (e.g. 'Database Fundamentals', 'Data Structures').
Need a Hint?
Using SQL pattern matching:
  • In the WHERE clause, use the LIKE operator instead of =.
  • The % symbol represents zero, one, or multiple arbitrary characters. Placing % after a word matches anything starting with that prefix, while '%Data%' would match it anywhere in the text.
  • Select only the two requested columns, not *. Note that LIKE case sensitivity depends on the database (case-insensitive in SQLite and MySQL, case-sensitive in PostgreSQL).

Schema Model • Members Entity

erDiagram Members { int id PK varchar_100 fullName "NOT NULL" varchar_1 membershipType "CHECK ('S', 'F', 'G')" int joinYear "CHECK >= 2000" }
Task 4: Computed Columns Pending

Calculate Student Membership Tenure

Write a query on the Members table:
  • Project fullName and a computed column calculating active tenure as 2026 - joinYear.
  • Assign the alias membershipYears to this computed column.
  • Filter strictly for student members (membershipType = 'S').
Need a Hint?
Expressions and column renaming:
  • Mathematical operations can be placed directly in the SELECT projection list: SELECT col, (2026 - col2) AS alias_name FROM .... Use AS to name the computed column.
  • Filter text matches using single quotes in the predicate: WHERE membershipType = 'S'.

Schema Model • Members Entity

erDiagram Members { int id PK varchar_100 fullName "NOT NULL" varchar_1 membershipType "CHECK ('S', 'F', 'G')" int joinYear "CHECK >= 2000" }
Task 5: Three-Valued Logic Pending

Filter with IS NULL & Disjunction

Write a query on the Members table to inspect edge entries:
  • Select all columns (*).
  • Filter for records where the joinYear is unassigned (NULL) OR where the membershipType is equal to 'G' (Guest).
Need a Hint?
Testing for NULL:
  • In SQL, = NULL always evaluates to UNKNOWN. Always write column_name IS NULL.
  • Combine alternative conditions with the OR keyword.

Schema Model • Books Entity

erDiagram Books { int id PK varchar_150 title "NOT NULL" varchar_20 isbn "UNIQUE NOT NULL" int publicationYear "CHECK > 1800" int totalCopies "DEFAULT 1" }
Task 6: Range & Sets Pending

Compound Ranges & Ordering

Query the Books table to extract publication cohorts:
  • Select all columns (*).
  • Filter for books published either between 1990 and 2020 inclusive (using BETWEEN) OR having a publicationYear in the historic release set (1850, 1875) (using IN).
  • Sort the results by publicationYear in descending order (DESC).
Need a Hint?
Combining ranges, sets, and sorting:
  • Use BETWEEN low AND high for continuous inclusive intervals.
  • Use IN (val1, val2) for explicit discrete sets.
  • Join the two conditions with OR, since a book needs to satisfy only one of them.
  • Append ORDER BY column_name DESC as the final clause of your query.

Schema Model • Loans Entity

erDiagram Loans { int id PK varchar_20 isbn "NOT NULL" int memberId "NOT NULL" int durationDays "DEFAULT 14" int overdueDays }
Task 7: Aggregation Pending

Table-Wide Summary Statistics

Query the Loans table to compute baseline metrics across all loans:
  • Calculate the total number of all loan records using COUNT(*).
  • Calculate the count of loans that have a recorded value for overdueDays (excluding NULLs) using COUNT(overdueDays).
  • Calculate the overall average loan duration using AVG(durationDays).
Need a Hint?
Aggregate function mechanics:
  • COUNT(*) counts every row in the table (or in each group), including rows containing NULLs.
  • COUNT(columnName) counts non-null occurrences in that specific column.
  • AVG(columnName) calculates the mathematical mean of all non-null numbers in that column.

Schema Model • Members Entity

erDiagram Members { int id PK varchar_100 fullName "NOT NULL" varchar_1 membershipType "CHECK ('S', 'F', 'G')" int joinYear "CHECK >= 2000" }
Task 8: Grouping Pending

Count Members by Membership Type

Write a grouped summary query on the Members table:
  • Select membershipType.
  • Count the number of members in each category aliased as memberCount.
  • Group the rows by membershipType.
Need a Hint?
Grouping categorical data:
  • Every unaggregated column selected in the SELECT clause must appear in the GROUP BY clause.
  • Follow the template: SELECT category_col, COUNT(*) AS alias FROM table GROUP BY category_col;.
  • Rows with a NULL category are not dropped: all NULLs form one group of their own.

Schema Model • Loans Entity

erDiagram Loans { int id PK varchar_20 isbn "NOT NULL" int memberId "NOT NULL" int durationDays "DEFAULT 14" int overdueDays }
Task 9: HAVING Filter Pending

Filter Aggregates for Chronic Overdue Borrowers

Query the Loans table to find repeat overdue borrowers:
  • Select memberId, the total overdue loan count aliased as loanCount, and the average overdue days aliased as avgOverdue.
  • Only consider records that were actually overdue (where overdueDays > 0).
  • Group the data by memberId.
  • Use HAVING to only display members who have strictly more than 2 such overdue loans.
Need a Hint?
The fundamental WHERE vs HAVING distinction:
  • WHERE evaluates raw rows before grouping occurs (use it for overdueDays > 0).
  • HAVING evaluates grouped results after aggregation (use it for COUNT(*) > 2).
  • Aggregate functions are not allowed in WHERE (WHERE COUNT(*) > 2 is an error).
  • Use the required aliases loanCount and avgOverdue for the two aggregate columns.

Schema Model • Members Entity

erDiagram Members { int id PK varchar_100 fullName "NOT NULL" varchar_1 membershipType "CHECK ('S', 'F', 'G')" int joinYear "CHECK >= 2000" }
Task 10: DML Maintenance Pending

Update & Delete Member Data

Write two maintenance statements for Members separated by semicolons:
  1. Update: Set membershipType = 'F' for all members who joined before 2010 (joinYear < 2010).
  2. Delete: Remove members whose joinYear IS NULL AND whose membershipType is 'S'.
Need a Hint?
Targeted modifications and deletions:
  • Updates follow: UPDATE TableName SET column = 'newValue' WHERE condition;.
  • Deletions follow: DELETE FROM TableName WHERE col IS NULL AND col2 = 'val';.
  • Always check for null values using IS NULL, not equality.
  • End each statement with a semicolon. Rows with a NULL joinYear are never matched by joinYear < 2010 (the comparison is UNKNOWN), so the UPDATE leaves them alone.

🎉 Practicum Completed Successfully!

All 10 SQL exercises have been verified. The lab stopwatch has stopped. You can now print or export your official Certificate of Completion.

Certificate of Completion

SQL CRUD & Query Practicum • Database Systems

This certifies that
[Student Name]
Student Identification Number: [Student ID]

has successfully implemented, analyzed, and manipulated single-table database structures conforming to ER model standards, demonstrating mastery across table definition (DDL), record insertion with default resolution, compound analytical queries, grouped aggregations, and row-level maintenance operations.

Time Expended: --
Completion Date: --
Verification Code: --