521 HW3
521 HW3
INSERT INTO takes (ID, course_id, sec_id, semester, year, grade) VALUES
('00128', 'CS-101', '1', 'Fall', 2017, 'A'),
('00128', 'CS-347', '1', 'Fall', 2017, 'A'),
('12345', 'CS-101', '1', 'Fall', 2017, 'C'),
('12345', 'CS-190', '2', 'Spring', 2017, 'A'),
('12345', 'CS-315', '1', 'Spring', 2018, 'A'),
('12345', 'CS-347', '1', 'Fall', 2017, 'A'),
('19991', 'HIS-351', '1', 'Spring', 2018, 'B'),
('23121', 'FIN-201', '1', 'Spring', 2018, 'C+'),
('44553', 'PHY-101', '1', 'Fall', 2017, 'B'),
('45678', 'CS-101', '1', 'Fall', 2017, 'F'),
('45678', 'CS-101', '1', 'Spring', 2018, 'B+'),
('45678', 'CS-319', '1', 'Spring', 2018, 'B'),
('54321', 'CS-101', '1', 'Fall', 2017, 'A'),
('54321', 'CS-190', '2', 'Spring', 2017, 'B+'),
('55739', 'MU-199', '1', 'Spring', 2018, 'A'),
('76543', 'CS-101', '1', 'Fall', 2017, 'A'),
('76543', 'CS-319', '2', 'Spring', 2018, 'A'),
('76653', 'EE-181', '1', 'Spring', 2017, 'C'),
('98765', 'CS-101', '1', 'Fall', 2017, 'C'),
('98765', 'CS-315', '1', 'Spring', 2018, 'B'),
('98988', 'BIO-101', '1', 'Summer', 2017, 'A'),
('98988', 'BIO-301', '1', 'Summer', 2018, NULL);
Table 2 : PREREQ
Basic Queries
1. Selecting Attributes:
Select specific attributes from a table.
SELECT dept_name from instructor
2.String operations
select name from instructor where name like '%dar%'
3.Ordering the results
select distinct name from instructor order by name desc;
• Tuple Comparision :
select name, course_id from instructor, teaches where (instructor.ID, dept_name) = (teaches.ID,
'Biology');
4. Aggregate functions
• Find the average salary of instructors in the Computer Science department
select avg (salary) from instructor where dept_name= 'Comp. Sci.';
• Find the total number of instructors who teach a course in the Spring 2018 semester
select count (distinct ID) from teaches where semester = 'Spring' and year = 2018;
Direct tuple conversion is actually not supported by SQL server
• Find the names and average salaries of all departments whose average salary is greater than 42000
select dept_name, avg (salary) as avg_salary from instructor group by dept_name having avg (salary) >
42000;
Set Operations
• Find courses that ran in Fall 2017 or in Spring 2018
(select course_id from section where sem = 'Fall' and year = 2017) union (select course_id from section
where sem = 'Spring' and year = 2018)
Set Membership
• Find courses offered in Fall 2017 and in Spring 2018
select distinct course_id from section where semester = 'Fall' and year= 2017 and course_id in (select
course_id from section where semester = 'Spring' and year= 2018);
• Name all instructors whose name is neither “Mozart” nor Einstein” select distinct name from
instructor where name not in ('Mozart', 'Einstein'):
• Find the total number of (distinct) students who have taken course sections taught by the instructor
with ID 10101
select count (distinct ID) from takes where (course_id, sec_id, semester, year) in (select course_id,
sec_id, semester, year from teaches where teaches.ID= 10101);
Direct tuple conversion is actually not supported by SQL server
Set Comaprision:
Find names of instructors with salary greater than that of some (at least one) instructor in the Biology
department.:
select distinct T.name
from instructor as T, instructor as S
where T.salary > S.salary and S.dept_name = 'Biology';
• All clause;
Find the names of all instructors whose salary is greater than the salary of all instructors in the
Biology department
select name
from instructor
where salary > all (select salary
from instructor
where dept_name = 'Biology');
With Clause:
Find all departments with the maximum budget
with max_budget (value) as
(select max(budget)
from department)
select department.dept_name
from department, max_budget
where department.budget = max_budget.value;
Scalar subquery
select dept_name,
( select count(*) from instructor
where department.dept_name = instructor.dept_name)
as num_instructors
from department;
Data Modifications
Insert and Delete
• delete from instructor
• delete from instructor where dept_name = 'Finance';
• delete from instructor
where dept_name in (select dept_name from department where building = 'Watson');
• delete from instructor where salary < (select avg (salary) from instructor);
• insert into course values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);
• insert into student values ('3003', 'Green', 'Finance', null);
• insert into instructor select ID, name, dept_name, 18000 from student where dept_name =
'Music' and total_cred > 144;
The above queries couldnot be executed because they conflict with the foreign key constraints
Update
• update instructor set salary = salary * 1.05
• update instructor set salary = salary * 1.05 where salary < 70000;
• update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end
• UPDATE student
SET tot_cred = (
SELECT
CASE
WHEN SUM(course.credits) IS NOT NULL THEN SUM(course.credits)
ELSE 0
END
FROM takes
INNER JOIN course ON takes.course_id = course.course_id
WHERE takes.ID = student.ID
AND takes.grade <> 'F'
AND takes.grade IS NOT NULL
);
Join Operations
• Natural Join in SQL:
select std_name, course_id
from student, takes
where student.ID = takes.ID;
View operations
• Create a view :
GO
CREATE VIEW faculty
AS
SELECT ID, name, dept_name
FROM instructor;
GO
• Using views
Find all instructors in the Biology department
select name from faculty where dept_name = 'Biology'
• View Expansion
DROP VIEW IF EXISTS physics_fall_2017_watson;
GO
create view physics_fall_2017_watson
as
select course_id, room_number
from physics_fall_2017
where building= 'Watson'
GO
• Update of view
insert into faculty values ('30765', 'Green', 'Music');
Integrity constraints
Create Table takes (
ID VARCHAR(5) NOT NULL,
course_id VARCHAR(8) NOT NULL,
sec_id VARCHAR(8) NOT NULL,
semester VARCHAR(6) NOT NULL,
course_year NUMERIC(4, 0) NOT NULL,
grade VARCHAR(2) CHECK (grade IN ('A', 'B', 'C', 'D', 'F')) DEFAULT 'F',
PRIMARY KEY (ID, course_id, sec_id, semester, course_year),
FOREIGN KEY (course_id, sec_id, semester, course_year)
REFERENCES section(course_id, sec_id, semester, course_year)
ON DELETE CASCADE,
FOREIGN KEY (ID)
REFERENCES student(ID)
ON DELETE CASCADE, CHECK (semester IN ('Fall', 'Winter', 'Spring', 'Summer')),
);
User Defined Types
Create :
CREATE TYPE Dollars FROM NUMERIC(12, 2) NOT NULL;
Using UDT :
create table department_test
(dept_name varchar (20),
building varchar (15),
budget Dollars);
Domains
• create type person_name from char(20) not null
• create domain degree_level varchar(10) constraint degree_level_test check (value in
('Bachelors', 'Masters', 'Doctorate'))
Create Domain doesn’t work in Microsoft SQL server
Performance and Indexing
I chose to create an index file for Department table.
The below function has randomly inserted a huge number of values into the Department table.
DECLARE @i INT = 1;
SET @i = @i + 1;
END;
Authorization :
• Create Users
CREATE LOGIN Amit WITH PASSWORD = 'SecurePassword123';
CREATE USER Amit FOR LOGIN Amit;
Grant Privileges
GRANT SELECT ON department TO Amit, Satoshi;
Revoke Privileges
REVOKE SELECT ON student FROM U1, U2;
Authorising views :
CREATE VIEW geo_instructor AS
SELECT *
FROM instructor
WHERE dept_name = 'Geology';