levo

Fundamentals of Databases

Structured Query Language (SQL)

  • 3 things to know
  • Paper 2

Overview

Recall questions on SELECT, FROM, WHERE and ORDER BY, joining tables on their keys, LIKE and its wildcards, and writing INSERT, UPDATE and DELETE statements.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Process

What does each clause do in a SELECT that filters by date and sorts the output?

Process

Write a query listing the title and borrower surname of every book still on loan.

Evaluation

Why must a link table appear in the FROM clause even when none of its columns are shown?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is SQL used for?

    Model answer

    SQL is a language for working with the data in a relational database. It is used to retrieve data, to update existing data, to insert new data and to delete data, in each case across one or more tables, and it is also used to define the structure of a database table in the first place. The user states what data is wanted rather than how to find it, and the database management system works out how to carry the request out.

  2. 2

    State the parts of a SELECT statement used to retrieve data, and what each part does.

    Model answer

    SELECT lists the attributes to be returned, or an asterisk for all of them. FROM names the table or tables the data comes from. WHERE gives a condition that a record must satisfy to be included, and is also where tables are matched to one another. ORDER BY names the attribute or attributes the results are sorted by, with ASC for ascending — the default — or DESC for descending. A statement ends with a semicolon. Only SELECT and FROM are required.

  3. 3

    Write SQL to list the surname and telephone number of every member who joined before 1 January 2020, in alphabetical order of surname, from a table Member(MemberID, Surname, Forename, Telephone, DateJoined).

    Model answer

    SELECT Surname, Telephone FROM Member WHERE DateJoined < '2020-01-01' ORDER BY Surname ASC; The attributes wanted are named after SELECT rather than using an asterisk, so only those two columns are returned; the WHERE condition compares the date attribute with a date literal; and ORDER BY sorts the output, ASC being the default and therefore optional.

  4. 4

    How do you retrieve data from more than one table in a single SELECT statement?

    Model answer

    Name every table needed in the FROM clause, and give a condition matching the foreign key in one table against the primary key in the other, so that only rows which genuinely belong together are combined. Where an attribute name appears in more than one table, qualify it with the table name and a full stop. For example: SELECT Member.Surname, Loan.DateDue FROM Member, Loan WHERE Member.MemberID = Loan.MemberID; The same query can be written using the join syntax: SELECT Member.Surname, Loan.DateDue FROM Member INNER JOIN Loan ON Member.MemberID = Loan.MemberID;

  5. 5

    What happens if you list two tables in a FROM clause but leave out the condition matching their keys?

    Model answer

    Every row of the first table is combined with every row of the second, producing all possible pairings rather than the meaningful ones. With 500 members and 2000 loans that is a million rows, almost all of them pairing a member with a loan that is not theirs. The result is not merely slow but wrong, so the matching condition is essential, not an optimisation.

  6. 6

    Write SQL to list the title of each book currently on loan together with the surname of the borrower, given Book(BookID, Title, AuthorID), Member(MemberID, Surname, Forename) and Loan(LoanID, BookID, MemberID, DateOut, DateReturned).

    Model answer

    SELECT Book.Title, Member.Surname FROM Book, Member, Loan WHERE Loan.BookID = Book.BookID AND Loan.MemberID = Member.MemberID AND Loan.DateReturned IS NULL ORDER BY Member.Surname; Three tables are needed because the link between Book and Member runs through Loan, so two matching conditions are required — one for each relationship. IS NULL selects loans with no return date recorded, which are the ones still out.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Fundamentals of Databases, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.