0% found this document useful (0 votes)
60 views4 pages

CPSC 471 HW 3

This document contains the SQL queries for homework 3 submitted by Aditi Yadav. It includes queries to select data from related tables by joining on foreign keys and filtering results where specific conditions are met. The queries retrieve employee names and details, courses taught by a given instructor, student information based on class year and major, and more. It also defines the schema for tables to store employee, student, city and playground data with foreign key relationships.

Uploaded by

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

CPSC 471 HW 3

This document contains the SQL queries for homework 3 submitted by Aditi Yadav. It includes queries to select data from related tables by joining on foreign keys and filtering results where specific conditions are met. The queries retrieve employee names and details, courses taught by a given instructor, student information based on class year and major, and more. It also defines the schema for tables to store employee, student, city and playground data with foreign key relationships.

Uploaded by

aditiyv
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CPSC 471 HW 3 (Aditi Yadav, UCID: 30143652)

(a) SELECT fname, lname


FROM employee
INNER JOIN works_on ON employee.ssn = works_on.essn
INNER JOIN project ON works_on.pno = project.pnumber
WHERE dno = 5 AND hours > 10 AND pname = 'ProductX';

(b) SELECT E.Fname, E.Lname


FROM EMPLOYEE E
INNER JOIN DEPENDENT D ON E.Ssn = D.Essn
WHERE E.Fname = D.Dependent_name;

(c) SELECT E.Fname, E.Lname


FROM EMPLOYEE E
INNER JOIN EMPLOYEE S ON E.Super_ssn = S.Ssn
WHERE S.Fname = 'Franklin' AND S.Lname = 'Wong';
(b) SELECT C.Course_name
FROM COURSE C
JOIN SECTION S ON C.Course_number = S.Course_number
WHERE S.Instructor = 'King' AND (S.Year = '07' OR S.Year = '08');

(d) SELECT ST.Name, CO.Course_name, CO.Course_number, CO.Credit_hours,


SE.Semester, SE.Year, GR.Grade
FROM STUDENT ST
JOIN GRADE_REPORT GR ON ST.Student_number = GR.Student_number
JOIN SECTION SE ON GR.Section_identifier = SE.Section_identifier
JOIN COURSE CO ON SE.Course_number = CO.Course_number
WHERE ST.Class = 4 AND ST.Major = 'CS';

PART 2
1. Employee

CREATE TABLE Employee (


SIN INT PRIMARY KEY,
name VARCHAR(255),
age INT,
sex CHAR(1),
phone VARCHAR(15),
city_name VARCHAR(255),
FOREIGN KEY (city_name) REFERENCES City(name)
);

2. Kid

CREATE TABLE Kid (


SIN INT PRIMARY KEY,
name VARCHAR(255),
age INT,
p1_sin INT,
p2_sin INT,
playground_name VARCHAR(255),
FOREIGN KEY (p1_sin) REFERENCES Employee(SIN),
FOREIGN KEY (p2_sin) REFERENCES Employee(SIN),
FOREIGN KEY (playground_name) REFERENCES Playground(name)
);

3. Playground

CREATE TABLE Playground (


name VARCHAR(255) PRIMARY KEY,
street_no INT,
city_name VARCHAR(255),
zip_code VARCHAR(10),
FOREIGN KEY (city_name) REFERENCES City(name)
);

4. City

CREATE TABLE City (


name VARCHAR(255) PRIMARY KEY,
area DECIMAL(10,2),
population INT,
province VARCHAR(255),
country VARCHAR(255)
);
PART 3

(a) SELECT name


FROM City
WHERE name NOT IN (SELECT city_name FROM Playground);

(b) SELECT name, population


FROM City
WHERE population = (SELECT MIN(population) FROM City);

(c) SELECT DISTINCT E.SIN, E.name


FROM Employee E
JOIN Kid K ON E.SIN = K.p1_sin OR E.SIN = K.p2_sin
WHERE K.playground_name IS NOT NULL;

(d) SELECT P.name


FROM Playground P
WHERE NOT EXISTS (
SELECT *
FROM Kid K
WHERE K.playground_name = P.name AND K.city_name <> P.city_name
);

(e) SELECT E.SIN, E.name


FROM Employee E
WHERE NOT EXISTS (
SELECT P.name
FROM Playground P
WHERE NOT EXISTS (
SELECT K.SIN
FROM Kid K
WHERE K.playground_name = P.name AND (K.p1_sin = E.SIN OR K.p2_sin = E.SIN)
)
);

You might also like