Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Week 2 Tutorial

Name: Dhugraj Gurung

Group: C5

Question 1

Assume that part of a database contained the following details:

• Customer (cust-no, cust-name, address, postal-area)


• Order (order-no, order-date, cust-no, order-total)
• Order-line (order-no, prod-no, ord-qty, line-total)
• Product (prod-no, prod-desc, unit-price)

For each of the following queries, write a separate solution in SQL:

(a) Display a list of products, including the description and price.


select * from product;
(b) Display a list of products which are prices below ‘200’.
select * from order
where unit-price < 200;
(c) List the names of customers who live in the ‘NW’ postal area.
select cust-name
from customer
where postal-area = ‘NW’;
(d) Display orders placed by customers ‘100’.
select * from order
where cust-no = 100;
(e) Find products which have been ordered in a quality of more than ‘50’.
select prod-no, prod-desc
from order-line join product
on order-line.prod-no = product.prod-no
where ord-qty > 50;
(f) Display a list of products numbers which have been ordered by customer ‘100’ between
’01-JAN-07’ and ’31-JAN-07’.
select prod-no
from orderline join order
on order-line.order-no = order.order-no
where cust-no = 100 and
ord-date between '01-JAN-07' and '31-JAN-07';
(g) Find the cust-no, cust-name, order-no, order-date, order-total, prod-no, prod-desc, for all
orders where the order-total for the order is greater than ‘5000’.

select cust-no, cust-name. order-no, order-date, order-total,


prod-no, prod-desc

from customer join order

on customer.cust-no = order.cust-no

join order-line

on order.order-no = order-line.order-no

join product

on order-line.prod-no = product.prod-no

where order-total > 5000;

Question 2

(a) Explain the terms Data Definition Language and Data Manipulation Language. As part of
your explanation you should provide knowledge of the use of at least 3 commands for each
language using SQL notation.
Data Definition Language (DDL)
Data Definition Language (DDL) statements are used to classify the database structure or
schema. It is a type of language that allows the DBA or user to depict and name those
entities, attributes, and relationships that are required for the application along with any
associated integrity and security constraints. The commands used under the DDL are as
follows;
• CREATE - used to create objects in the database
• ALTER - used to alters the structure of the database
• DROP - used to delete objects from the database

Data Manipulation Language (DML)

A language that offers a set of operations to support the fundamental data manipulation
operations on the data held in the database is called Data Manipulation Language (DML).
Data Manipulation Language (DML) statements are used to manage data within schema
objects. Here are the lists of tasks that come under DML:

• SELECT - It retrieves data from a database


• INSERT - It inserts data into a table
• UPDATE - It updates existing data within a table
(b) Consider the following relations: DOCTOR (doctor-no, doctor-name, doctor-position,
base-hospital) PATIENT (patient-no, patient-name, patient-type, gender) TREATMENT
(doctor-no, patient-no, drug) Give SQL formulations for the following queries:
i. Find the names of doctors who have the position of 'Consultant'.
select doctor-name
from doctor
where doctor-position = 'Consultant';
ii. Find the drugs which patient ‘Jones’ has been prescribed.
select drug
from treatment join patient
on treatment.patient-no = patient.patient-no
where patient-name = 'Jones';
iii. Find the drugs that patient ‘Jones’ has been prescribed by doctor-no ‘D100’.
select drug
from treatment join patient
on treatment.patient-no = patient.patient-no
where patient-name = ‘Jones’ and
doctor-no = ‘D100’;
iv. Find the drugs that doctor ‘Smith’ prescribes for his patients.
select drug
from treatment join doctor
on treatment.doctor-no = doctor.doctor-no
where doctor-name = 'Smith';

You might also like