DBMS Practice Questions-SQL
Name: Shivani Joshi
Roll No.: 2K20/UMBA/38
Q1 : Write MySQL queries for the following:
CREATE A DATABASE, TABLE
Query
create database work;
use work;
create table Datasheet(
Name char(20) not null,
Sid int not null primary key,
course char(20) not null
);
Output
DESCRIBE THE DATABASES AND TABLES
Query
desc datasheet;
Output
INSERT THE VALUES IN TABLE
Query
insert into datasheet values ('Ram',30, 'Operations');
insert into datasheet values ('Sham',20, 'IT');
insert into datasheet values ('Dham',10, 'Finance');
insert into datasheet values (Manu,40, 'HR');
insert into datasheet values (Shamu,50, 'IT');
insert into datasheet values (Nanu,60, 'IT');
TO VIEW THE CONTENTS OF THE TABLE
Query
Select * from datasheet;
Output
UPDATE THE CONTENTS OF A TABLE
Query
SET SQL_SAFE_UPDATES = 0;
Update datasheet
set course =’Marketing'
where Name='Shamu';
Select * from datasheet;
MODIFY THE STRUCTURE OF THE TABLES
Query
alter table datasheet add course_code int;
Select * from datasheet;
Output
TO RENAME THE TABLE
Query
alter table Rename datasheet to list;
Output
TO FIND DISTINCT VALUES
Query
Select distinct course from student_list;
LOGICAL OPERATORS (AND, OR ETC.)
MINIMUM, MAXIMUM, AVERAGE, SUM, COUNT, BETWEEN, LIKE
Query
Select Name, course from student_list;
where course = 'HR';
Select Name, course from student_list;
where course = 'HR' or course = 'IT';
Select * from student_list;
where course = 'finance' AND dham=’10';
Select min(sid) from student_list;
Select max(sid) from student_list;
Select avg(sid) from student_list;
Select sum(sid) from student_list;
Select count(sid) from list;
SELECT * FROM student_list;
WHERE Name LIKE 'S%';
Select * From student_list;
where Sid BETWEEN 5 AND 40;
Output
DELETE DATA FROM TABLE (ALL DATA , DELETE A PARTICULAR ROW)
Query
delete from list where Name='sham';
Select * from student_list;
DROP TABLE
Query
drop table student_list;
Q2. Write MySQL queries to apply following constraints on tables
Not NULL
UNIQUE
Primary Key
create database USME;
use USME;
create table SecA(
Name char(20) not null,
Sid int not null primary key,
course char(20) not null,
Aadhar_no int not null unique
);
Output
Foreign Key
alter table Employees add constraint fk foreign key(Depno) references Dept(dnumber);
Output
Q3. Create the following tables with requisite constraints. Insert data the
tables accordingly.
Employee( Firstname, Lastname, Empid, DOB, Address, city, Salary, Sex,
Super_id, Depno)
Department( Dept-name, dnumber, Mgr-id, Mgr-start-date) Project(Projname,
Projno, proj_location, Dno)
Works_on( Eid, Pno, Hrs_worked)
Write queries for data manipulation, aggregate functions and sorting concepts
for above tables.
create table Employees(
Firstname char(20) not null,
Lastname char(20) not null,
Empid int not null primary key,
DOB int not null,
Address varchar(30) not null,
city char(20) not null,
Salary int not null,
Sex char(10) not null,
Super_id int not null,
Depno int not null);
create table Dept(
Dept_name char(20) not null,
dnumber int not null primary key,
Mgr_id int not null,
Mgr_start_date date not null);
create table Project(
Projname char(20) not null,
Projno int not null primary key,
Proj_location char(30) not null,
Dno int not null);
create table Works_on(
Eid int not null,
Pno int not null,
Hrs_worked int not null);
Queries for Foreign key
alter table Employees add constraint fk foreign key(Depno) references Dept(dnumber);
alter table Project add constraint fk2 foreign key(Dno) references Dept(dnumber);
alter table Works_on add constraint fk3 foreign key(Eid) references Employees(Empid);
alter table Works_on add constraint fk4 foreign key(Pno) references Project(Projno);
alter table Employees add constraint fk5 foreign key(Super_id) references
Employees(Empid);
alter table dept add constraint fk6 foreign key(Mgr_id) references Employees(Empid);
Find the average salary of all employees.
Select avg(Salary) from Employees;
Count total number of employees
Select count(Empid) from employees;
Find the details of the employees whose salary is maximum.
Select max(Salary) from Employees;
Create a table employee1 from employee with all the columns.
Create table employee1 as select * from employees;
desc employee1;
Create a table employee2 with first two columns
Create table employee2 as select Firstname, Lastname from employees;
desc employee2;
Update the employee details of the employee whose employee id is 1003.
SET SQL_SAFE_UPDATES = 0;
Update Employees
set Empid = 1
where Empid = 1003;
select * from Employees;
Display the details of the employees whose department lies in (1, 5).
Select Dept_name from Dept where dnumber between 1 and 5;
Display all employee with descending order of their department numbers.
select * from employees order by depno;