0% found this document useful (0 votes)
3 views

Datbase Sheet

The document outlines the functionalities of Data Manipulation Language (DML) and Data Definition Language (DDL) in SQL, including querying, inserting, deleting, and modifying data in databases. It details various SQL commands for creating tables, defining integrity constraints, and performing complex queries using joins, aggregations, and subqueries. Additionally, it provides examples of SQL queries for retrieving and manipulating data related to instructors, courses, and students.

Uploaded by

maanya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Datbase Sheet

The document outlines the functionalities of Data Manipulation Language (DML) and Data Definition Language (DDL) in SQL, including querying, inserting, deleting, and modifying data in databases. It details various SQL commands for creating tables, defining integrity constraints, and performing complex queries using joins, aggregations, and subqueries. Additionally, it provides examples of SQL queries for retrieving and manipulating data related to instructors, courses, and students.

Uploaded by

maanya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

DML -- provides the ability to query information from the database and to insert tuples into, delete tuples

from, and modify tuples in


the database. integrity – the DDL includes commands for specifying integrity constraints. View definition -- The DDL includes
commands for defining views. Transaction control –includes commands for specifying the beginning and ending of transactions.
Embedded SQL and dynamic SQL -- define how SQL statements can be embedded within general-purpose programming languages.
Authorization – includes commands for specifying access rights to relations and views. Different DBs implement different standards -
user manual. The DDL allows the specification of information about relations, including: SCHEMA of each relation, type of values
associated with each attribute, integrity constraints, set of indices to be maintained for each relation, security and authorization info
for each relation, physical storage structure of each relation on disk.
Domain Types : 1. char(n): Fixed length character string, n-user defined length. 2. varchar(n): Variable length character strings. 3. Int:
integer 4. smallint - both are machine dependent 5. numeric(p,d): Fixed point number, with user-specified precision of p digits, with d
digits to the right of decimal point. 6. real, double precision. Floating point and double-precision floating point numbers, with
machine-dependent precision. 7. float(n): Floating point number, with user-specified precision of at least n digits.
- create table: create table r(name of relation) (A1 D1, A2 D2, ..., An Dn,(integrity-constraint1),...,(integrity-constraintk))
- integrity constraints: primary key(A1,...,An) ; foreign key (Am, ..., An ) references r ; not null ; check (P)

- desc <table name> - gives the schema of the table


- insert into instructor values ('10211', 'Smith', 'Biology', 66000);
- delete : Remove all tuples from the student relation (eg: delete from student)
- drop table r
- alter table r add A D: All exiting tuples in the relation are assigned null as the
value for
the new attribute. alter table r drop A (not supported by many databases)
- select A1, A2, ..., An from r1, r2, ..., rm where P - The result of an SQL query is a
relation
- SQL names are case insensitive; distinct - to force the eliminations of duplicates; all-specifies that duplicates should not be removed
- An attribute can be a literal with no from clause (select '437' → Results is a table with one column and a single row with value “437”)
- Can give the column a name using: select '437' as FOO - An attribute can be a literal with from clause select 'A' from instructor →
table with one column and N rows (number of tuples in the instructors table), each row with value “A”
- +, –, ∗, and / (select ID, name, salary/12 from instructor) - select ID, name, salary/12 as monthly_salary
- logical connectives and, or, and not - comparison operators <, <=, >, >=, =, and <> - select name from instructor where dept_name
= 'Comp. Sci.' and salary > 80000 - from clause : cartesian product (select ∗ from instructor, teaches) - select name, course_id from
instructor , teaches where instructor.ID = teaches.ID → names of all instructors who have taught some course and the course_id
- select name, course_id from instructor , teaches where instructor.ID = teaches.ID and instructor. dept_name = 'Art' → names of all
instructors in the Art department who have taught some course and the course_id -as operator : old-name as new-name
- select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = 'Comp. Sci.’ → names of
all instructors who have a higher salary than some instructor in 'Comp. Sci' -Find the names of all instructors whose name includes the
substring “dar”. select name from instructor where name like '%dar%' -Match the string “100%” like '100 \%' escape '\'
- 'Intro%' matches any string beginning with “Intro”. '%Comp%' matches any string containing “Comp” as a substring. '_ _ _' matches
any string of exactly three characters. '_ _ _ %' matches any string of at least three characters. -select distinct name from instructor
order by name → alphabetic order the names of all instructors -desc -Can sort on multiple attributes:order by dept_name, name
- select name from instructor where salary between 90000 and 100000 -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) - Find courses that ran in Fall 2017 and in Spring 2018 (select course_id from section where sem = 'Fall' and year = 2017)
intersect (select course_id from section where sem = 'Spring' and year = 2018). -Find courses that ran in Fall 2017 but not in Spring
2018 (select course_id from section where sem = 'Fall' and year = 2017) except (select course_id from section where sem =
'Spring' and year = 2018) -5 + null returns null -select name from instructor where salary is null -predicate is not null
- Aggregate Functions: avg, min, max, sum, count -select dept_name, avg (salary) as avg_salary from instructor group by
dept_name; → the average salary of instructors in each department -select dept_name, avg (salary) as avg_salary from instructor​
group by dept_name having avg (salary) > 42000; →names and average salaries of all departments whose average salary is greater
than 42000 -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'; – select name from
instructor where salary > some (select salary from instructor where dept name = 'Biology'); -“Find all courses taught in both the Fall
2017 semester and in the Spring 2018 semester” select course_id from section as S where semester = 'Fall' and year = 2017 and
exists (select * from section as T where semester = 'Spring' and year= 2018 and S.course_id = T.course_id);
- Find the average instructors’ salaries of those departments where the average salary is greater than $42,000.” select dept_name,
avg_salary from ( select dept_name, avg (salary) as avg_salary from instructor group by dept_name) as T where avg_salary >
42000; OR select dept_name, avg_salary from ( select dept_name, avg (salary) from instructor group by dept_name) as dept_avg
(dept_name, avg_salary) where avg_salary > 42000;
- Natural join matches tuples with the same values for all common attributes, and retains only one copy of each common column. Outer
join - avoids loss of info, Computes the join and then adds tuples form one relation that does not match tuples in the other relation to
the result of the join. Uses null values.(left,right,full) - Join condition – defines which tuples in the two relations match, and what
attributes are present in the result of the join (inner,left,right,full outer). Join type – defines how tuples in each relation that do not
match any tuple in the other relation (based on the join condition) are treated (natural, on predicate,using(A1…An).

1. Find the names of all the instructors from Computer Science department or Biology department. - select name from instructor where
dept_name='Comp. Sci.' or dept_name='Biology'; 2. Find the names of courses run by the Biology department. - select title from course where
dept_name='Biology'; 3. Find the names and total credits taken by students who have taken at least 80 total credits. - select name, tot_cred
from student where tot_cred >= 80; 4.Show the names and salary of all instructors in Computer Science department in descending order of
their salary. - select name, salary from instructor where dept_name='Comp. Sci.' order by salary desc; 5. Find the location (building and room
number) of classrooms whose capacity is between 50 and 100. - select building, room_number from classroom where capacity >= 50 and
capacity <= 100; / select building, room_number from classroom where capacity between 50 and 100; 6. Find the total credits across all courses
offered by a particular department (say, Biology) - select sum(credits) from course where dept_name='Biology'; / Renaming: select sum(credits)
total_credits from course where dept_name='Biology'; 7. Find names of all courses whose prerequisite is a particular course (say, CS-101). -
select title from course, prereq where prereq_id='CS-101' and course.course_id=prereq.course_id; / select title from course natural join prereq where
prereq_id='CS-101'; / select title from course inner join prereq where prereq_id='CS-101' and course.course_id=prereq.course_id; 8. Find average
salary of an instructor. - select avg(salary) from instructor; 9. Find average salary of an instructor in Computer Science department. - select
avg(salary) from instructor where dept_name='Comp. Sci.'; 10. Show the names of instructors who earn more than the average salary of an
instructor in Computer Science department. - select name from instructor, (select avg(salary) as avg_sal_CS from instructor where
dept_name='Comp. Sci.') as T where salary > T.avg_sal_CS; / select name from instructor where salary > all (select avg(salary) from instructor where
dept_name = 'Comp. Sci.'); 11. Show the department name and the average salary in that department sorted by average salary. - select
dept_name, avg(salary) as avg_salary from instructor group by dept_name; 12. Show the student name and course title for students who got ‘A’
grade in that course. - select name, title from course, student, takes where grade='A' and student.ID = takes.ID and course.course_id =
takes.course_id; (without join) / select name, title from student natural join takes natural join course where grade='A'; (natural join) - this query is not
correct in general but works in this case because there’s an attribute - dept_name common in course and student also which we don’t need. 14. Find
the names of courses in Physics department which have 2 or 3 credits. - select title from course where dept_name = 'Physics' and (credits = 2 or
credits = 3); / mysql> select title from course where dept_name = 'Physics' and credits in (2,3); 15. For the student with ID 44553 (or any other
fixed value, say 45678), show course ID, title and credits of all courses registered for by the student. - select course_id, title, credits from takes
natural join course where ID = 44553; 16. For the student with ID 44553 (or any other fixed value, say 45678), show the total number of credits
of all courses by the student. - select sum(credits) from takes natural join course where ID = 12345; 17. For the student with ID 44553 (or any
other fixed value, say 45678), show the total number of credits of all courses by the student in Fall semester. - select sum(credits) from takes
natural join course where ID=45678 and semester = 'Fall'; / select sum(credits) from takes natural join course where ID=44553 and semester ='Fall';
18. Same as above three queries but also show the ID of the student along with total credits. - select distinct name from student natural join
takes, course where course.course_id = takes.course_id and course.dept_name = 'Comp. Sci.'; 19. Find the names of students who have taken
any Computer Science course. A student’s name should not be displayed more than once. - select ID from instructor where ID not in (select ID
from instructor natural join teaches); / select ID from instructor where ID except (select ID from instructor natural join teaches); 20. Display the IDs of
all instructors who have never taught a course. - select ID from instructor as T where not exists (select ID from instructor natural join teaches
where instructor.ID=T.ID); / select ID from instructor where ID not in (select ID from instructor natural join teaches); 21. Find the number of courses
offered by Computer Science department. - select count(*) from course where dept_name = 'Comp. Sci.'; 22. Find the number of courses
offered by Computer Science department in the Spring semester. Ensure that multiple offerings are not considered. - select count(distinct
course_id) from course natural join section where dept_name = 'Comp. Sci.' and semester = 'Spring'; 23. Display the IDs of all instructors who
have never taught a course in a Fall semester. 24. As above, but display the names of the instructors also, not just the IDs. - select ID / name
from instructor where ID not in (select ID from instructor natural join teaches where semester = 'Fall'); select ID / name from instructor as T where not
exists (select * from instructor natural join teaches where semester = 'Fall' and instructor.ID = T.ID); 25.For all students whose name has ‘an’ in it,
show their names and the names of courses taken by them. - select name, title from student natural join takes, course where course.course_id =
takes.course_id and name like '%an%'; 26. For all students whose name has ‘an’ in it, show the names of courses taken by them. A course’s
name should not be displayed more than once. - select distinct title from student natural join takes, course where course.course_id =
takes.course_id and name like '%an%'; 27. Find names of instructors who teach courses scheduled in Taylor building. Ensure that an
instructor’s name is not displayed more than once. - select distinct name from instructor natural join teaches natural join section where
building='Taylor'; 28. Same as above, but additionally display their salary. - select distinct name, salary from instructor natural join teaches natural
join section where building='Taylor'; 29. Find all courses whose identifier starts with the string ”CS-1”. - select * from course where course_id like
'CS-1%'; 30. Find names of instructors who have taught at least one of the above courses. - select name from instructor where ID in (select ID
from teaches natural join course where course_id like 'CS-1%'); / select name from instructor where ID = some (select ID from teaches natural join
course where course_id like 'CS-1%'); 31. Find title of courses that do not need any prerequisites. - select title from course as T where course_id
not in (select course_id from course natural join prereq where course.course_id = T.course_id); / select title from course as T where not exists (select
course_id from course natural join prereq where course.course_id = T.course_id); 32. Show the names of students and how many advisors they
have. - Wrong one (ignore that we are showing ID and not name): select s_ID, count(i_ID) from advisor natural join student group by s_ID; Almost
correct one: select s_ID, count(i_ID) from advisor, student where ID=s_ID group by s_ID; But, we want names: select name, count(i_ID) from advisor,
student where ID=s_ID group by s_ID 33. Show the names of instructors and how many advisees they have. - select name, count(s_ID) from
advisor, instructor where ID=i_ID group by i_ID; 34. Show the course ID and the enrollment count across all sections for all courses. - select
course_id, count(ID) from section natural join takes group by course_id; 35. Show the course ID and the enrollment count across all sections for
all courses which have at least 2 students enrolled. - select course_id, count(ID) from section natural join takes group by course_id having
count(ID) >= 2; 36. Show the maximum and minimum enrollment across all sections of all courses. - select max(T.cnt), min(T.cnt) from (select
count(ID) as cnt from section natural join takes group by course_id) as T; 37. For every section of each course, show the section details followed
by the enrollment count. What happens to sections when no student is enrolled for that section. - select course_id, sec_id, semester, year,
count(*) from takes natural join section group by course_id, sec_id, semester, year; 38. Show all sections with the maximum enrollment (along
with the enrollment), using a subquery. - select S.course_id, S.sec_id, S.semester, S.year from (select course_id, sec_id, semester, year, count(*)
as enrollment from takes group by course_id, sec_id, semester, year) as S, (select max(T.enrollment) as enroll from (select course_id, sec_id,
semester, year, count(*) as enrollment from takes group by course_id, sec_id, semester, year) as T) as Y where S.enrollment >= enroll;

You might also like