Lab Report 4 Final
Lab Report 4 Final
Santosh, Tangail-1902
Lab Report 04
Course Title: Database Management System Lab
Course Code: ICT-2108
Lab Report on: Basic Queries in MySQL
Submitted By Submitted To
Experiment No: 04
Experiment Name: Basic Queries in MySQL
Objectives: The main objective of this experiment is to use some basis queries of
MySQL. We will learn how to use query with different conditions to find desire output
from one or more database tables.
Required Instruments:
1. MySQL 8.3 Command line Client.
Queries:
The Cartesian product of the instructor relation with the teaches relation.
SQL expression: select name ,course_id from instructor ,teaches where
instructor.ID=teaches.ID;
Find only instructor names and course identifiers for instructors in the ICT department.
SQL expression: select name, course id from instructor, teaches where
instructor.ID= teaches.ID and instructor.dept_name = 'ICT';
If we want the attribute name name to be replaced with the name instructor_name , we can
rewrite the preceding query as:
SQL expression: select name as instructor_name, course id from
instructor, teaches where instructor.ID= teaches.ID;
For all instructors in the university who have taught some course, find their names and the
course ID of all courses they taught.
SQL expression: select T.name, S.course_id from instructor as T,
teaches as S where T.ID= S.ID;
Find the names of all instructors whose salary is greater than at least one instructor in the ICT
department.
SQL expression: select distinct T.name from instructor as T,
instructor as S where T.salary > S.salary and S.dept_name = 'ICT';
Find the names of all departments whose building name includes the substring '1’.
SQL expression: select dept name from department where building like
'%1%';
The asterisk symbol “ * ”can be used in the select clause to denote “all attributes. ”
SQL expression: select instructor.* from instructor, teaches where
instructor.ID= teaches.ID;
The order by clause causes the tuples in the result of a query to appear in sorted order.
SQL expression: select name from instructor where dept_name = 'ICT'
order by name;
We wish to list the entire instructor relation in descending order of salary. If several
instructors have the same salary, we order them in ascending order by name.
SQL expression: select * from instructor order by salary desc, name
asc;
If we wish to find the names of instructors with salary amounts between 30,000 and 45,000.
SQL expression: select name from instructor where salary between
30000 and 45000;
To find the set of all courses taught either in Fall 2017 or in Spring 2018, or both.
SQL expression: (select course_id from section where semester = 'Fall'
and year= 2017) union (select course_id from section where semester
= 'Spring' and year= 2018);
To find the set of all courses taught in both the Fall 2017 and Spring 2018.
SQL expression: (select course_id from section where semester = 'Fall'
and year= 2017) intersect (select course_id from section where
semester = 'Spring' and year= 2018);
To find all courses taught in the Fall 2017 semester but not in the Spring 2018 semester.
SQL expression: (select course_id from section where semester =
'Fall' and year= 2017) except (select course_id from section where
semester = 'Spring' and year= 2018);
[outputs on the next page]
Find the departments name and average salary where the average salary of the instructors is
more than 31,000.
SQL expression: select dept_name, avg(salary) as avg_salary from instructor group by
dept_name having avg(salary)>31000;
The names of instructors whose names are neither “MD.Alamin” nor “Fahim”.
SQL expression: select distinct name from instructor where name not in ('MD.Alamin' ,
'Fahim');
Discussion:
By the above queries, we can easily find out desired records and attributes. We can also use
arithmetic expressions, string operations, SQL aggregation functions etc. to find set of data as
per our expectations.