0% found this document useful (0 votes)
151 views2 pages

SQL Homework 7: Queries and Data

This document contains instructions for two SQL homework assignments. [1] The first assignment involves querying a database of cars for sale called "Cars" to return registration numbers that meet certain criteria based on mileage, year, transmission, make, and price. [2] The second assignment involves querying a database of school trips called "tblTrip" to return trip details where the number of students is greater than 60. It also involves querying a database of teachers called "tblTeacher" to return first and last names where the surname begins with B and the title is Mr. [3] Sample SQL queries are provided that can be used to complete the assignments by selecting the requested fields and applying the specified filters

Uploaded by

potholemary
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views2 pages

SQL Homework 7: Queries and Data

This document contains instructions for two SQL homework assignments. [1] The first assignment involves querying a database of cars for sale called "Cars" to return registration numbers that meet certain criteria based on mileage, year, transmission, make, and price. [2] The second assignment involves querying a database of school trips called "tblTrip" to return trip details where the number of students is greater than 60. It also involves querying a database of teachers called "tblTeacher" to return first and last names where the surname begins with B and the title is Mr. [3] Sample SQL queries are provided that can be used to complete the assignments by selecting the requested fields and applying the specified filters

Uploaded by

potholemary
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Homework 7 Introduction to SQL

Unit 6 Programming

Homework 7 Introduction to SQL


1. A secondhand car dealer keeps a database of cars for sale. A sample of the data is
shown below. The table below is named Cars.

a) State the registration number of the cars that will be found using each of the following
criteria:

Mileage < 20000 [1]


BH61LWZ, GF59NGB

Year >=2011 and Transmission = “Automatic” [1]

EF11GTZ, GR12JUK

(Make = “Peugeot” OR Make = “Renault”) AND (Net Selling Price <= 5200.00) [1]

CF11YHK, EF11GTZ

b) Write the SQL statement which will display the registration numbers and make of
all cars with 5 doors made in 2011 or after. [3]

SELECT CarRegistration, Make


FROM Cars
WHERE (Doors = 5) AND (Year >= 2011)
Homework 7 Introduction to SQL
Unit 6 Programming

2. A school keeps a record of all the school trips that take place each school year. An
extract from the table tblTrip is shown below:

NumberOf
Trip ID Description StartDate EndDate Destination TeacherInCharge
Students

DofE Gold
14 09/04/2014 13/04/2014 Wales 30 1453
Expedition

Year 7 Castles
15 01/05/2014 01/05/2014 Framlingham 87 1506
Trip

Year7
Walton-on-
16 Geography Field 07/05/2014 07/05/2014 91 3035
the-Naze
Trip
Prefects
17 Leadership 20/06/2014 23/06/2014 Bradwell 15 2278
weekend

Spanish Taster
18 26/06/2014 30/06/2014 Santander 27 1453
Trip

(a) Write an SQL query to find all trips in tblTrip where the number of students is greater
than 60. The Results table should display the columns labelled Description, StartDate,
EndDate, Destination and NumberofStudents. [3]
SELECT Description, StartDate, EndDate, Destination, NumberOfStudents
FROM tblTrip
WHERE (NumberOfStudents > 60)

A second table tblTeacher holds the following data:

TeacherID Title FirstName Surname

1453 Miss Catherine Black


1506 Mr Gerald Bainbridge
2278 Mr Robin McKay
3035 Mrs Amanda Briers

(b) Write an SQL statement to display Firstname and Surname of all teachers whose
surname begins with ‘B’ and whose title is ‘Mr’. [3]
SELECT FirstName, Surname
FROM tblTeacher
WHERE Surname LIKE ‘B*’ AND Title = ‘Mr’

[Total 12 marks]

Common questions

Powered by AI

Using a single condition in a WHERE clause specifies a straightforward filter, while combining multiple conditions with logical operators allows for more complex filtering criteria and precise data extraction. For example, combining conditions such as (Make = 'Peugeot' OR Make = 'Renault') AND (NetSellingPrice <= 5200.00) targets specific data patterns .

You could write a query such as: SELECT Description, StartDate, EndDate, Destination, NumberOfStudents FROM tblTrip WHERE (NumberOfStudents > specified_threshold). This approach allows dynamic modification of the threshold value for different query scenarios .

The LIKE keyword in SQL is used for pattern matching within text fields, enabling more flexible data querying. For instance, to find all teachers whose surnames start with 'B' and title is 'Mr', you can use: SELECT FirstName, Surname FROM tblTeacher WHERE Surname LIKE 'B%' AND Title = 'Mr' .

SELECT CarRegistration, Make FROM Cars WHERE (Doors = 5) AND (Year >= 2011).

In SQL, the AND operator requires all conditions to be true for a row to be selected, while the OR operator requires only one condition to be true. To select cars that are either of make 'Peugeot' or 'Renault' and have a net selling price of 5200.00 or less, you use: SELECT CarRegistration, Make FROM Cars WHERE (Make = 'Peugeot' OR Make = 'Renault') AND (NetSellingPrice <= 5200.00).

SELECT CarRegistration, Make FROM Cars WHERE (Make = 'Peugeot' OR Make = 'Renault') AND (NetSellingPrice <= 5200.00).

SELECT FirstName, Surname FROM tblTeacher WHERE Surname LIKE 'B%' .

Using specific column filters in SQL queries reduces data transfer time and memory usage, leading to faster query execution and more manageable results. For example, retrieving only necessary fields, such as CarRegistration and Make from the Cars table, minimizes resource consumption and highlights relevant data .

SELECT Description, StartDate, EndDate, Destination, NumberOfStudents FROM tblTrip WHERE (NumberOfStudents > 60).

Logical operators like AND, OR allow the combination of multiple conditions in SQL queries to precisely filter data according to complex criteria. For example, to find cars manufactured in 2011 or later with automatic transmission, you can use: SELECT CarRegistration, Make FROM Cars WHERE (Year >= 2011) AND (Transmission = 'Automatic').

You might also like