5 DBMS
5 DBMS
Practice problems:
1. Display the last 3 characters of the student name in uppercase directly followed by
the
job.
Ex: select lower(substr(ename,-3,3)),job from student;
2. Display all the students who belong to CSE dept.
3. List the emps whose names are same as SMITH or ALLEN.
4. How many students are there in the dept? Hint: use count() function
5. How many students are there in each department?
6. Display the students who do not enroll for any course.
7. List the students details who belong to either CSE or AIML branch.
8. Which department has highest number of students, show the department number and
the count?
9. Display the unique course names.
10. List the details of the students who attends college from ‘gandipet’.
11. Find all the employees whose name starts with ‘S’ and has 6 characters in the name.
12. Find out all those instructors who are drawing salary more than 10,000
Sol:
Problem Statement:
To create a tables using SQL with specified information such as table name, column
names, data types, and constraints, also insert data into the tables and retrieve specific data
from the created tables.
Description:
➢ Open the mysql site and create a new worksheet
➢ Create a table named "Student" with columns "roll number", "name varchar(30)",
"deptno number", "dept varchar(10)", "address varchar(20)", and "job varchar(50)". It
also creates tables "Employee", "Instructor", and "Enrollment" with respective
columns.
➢ Insert data into the "Student" table using the "insert into" statement. It inserts data for
columns "roll", "name", "deptno", "dept", "address", and "job".
➢ Insert data into the "Employee" table using the "insert into" statement. It inserts data
for columns "id" and "name".
➢ Insert data into the "Instructor" table using the "insert into" statement. It inserts data
for columns "name" and "salary".
➢ Insert data into the "Enrollment" table using the "insert into" statement. It inserts data
for columns "course" and "std_id".
➢ The code executes several SQL select statements to retrieve data from the tables.
➢ The first select statement selects the last three characters of each student's name and
their job title, and converts the characters to uppercase.
➢ The second select statement retrieves all data for students in the "CSE" department.
➢ The third select statement retrieves all data for employees with names "Smith" and
"Allen".
➢ The fourth select statement counts the number of students in the "CSE" department.
➢ The fifth select statement groups students by department and retrieves the department
and count of students in each department.
➢ The sixth select statement retrieves data for students not enrolled in any course.
➢ The seventh select statement retrieves data for students in the "CSE" and "AIML"
departments.
➢ The eighth select statement groups students by department and department number,
counts the number of students in each group, and retrieves the group with the highest
count.
➢ The ninth select statement retrieves the distinct courses in the "Enrollment" table.
➢ The tenth select statement retrieves data for students with the address "Gandipet".
➢ The eleventh select statement retrieves data for employees with names starting with
"S" and followed by any five characters.
➢ The twelfth select statement retrieves data for instructors with salaries greater than
10000.
Concepts used:
Syntaxes used for
→ Creating table:
create table table_Name( column1Name datatype,col2Name datatype……colnName
datatype);
→ Inserting data into tables:
insert into
tableName(col_1name,col_2name…col_nName)values(val_for_col1,val_for_col2…v
al_for_coln);
Code:
create table Student(roll number,name varchar(30),deptno number,dept varchar(10),address
varchar(20),job varchar(50));
create table Employee(id number,name varchar(30));
create table Instructor(name varchar(30),salary number);
create table Enrollment(course varchar(25),std_id number);
insert into Student(roll,name,deptno,dept,address,job) values
(101,'Sharavan',300,'CSE','Gandipet','Software Engineer');
insert into Student(roll,name,deptno,dept,address,job) values
(102,'Sowjanya',400,'ECE','Uppal','IAS');
insert into Student(roll,name,deptno,dept,address,job) values
(103,'Roshan',500,'AIML','Mehdipatnam','Data Scientist');
insert into Student(roll,name,deptno,dept,address,job) values
(104,'Karthik',500,'AIML','Kokapet','Web Developer');
insert into Student(roll,name,deptno,dept,address,job) values
(105,'Srinath',300,'CSE','Uppal','Database Administrator');
insert into Student(roll,name,deptno,dept,address,job) values
(106,'Santosh',600,'MECH','Gandipet','Automotive Engineer');
insert into Student(roll,name,deptno,dept,address,job) values
(107,'Naidu',300,'CSE','Alwal','Web Designer');
insert into Employee(id,name) values (1,'Shilpa');
insert into Employee(id,name) values (2,'Prasad');
insert into Employee(id,name) values (3,'Kittu');
insert into Employee(id,name) values (4,'Samuel');
insert into Employee(id,name) values (5,'Smith');
insert into Employee(id,name) values (6,'Allen');
insert into Instructor(name,salary) values ('Ram',150000);
insert into Instructor(name,salary) values ('Ravi',80000);
insert into Instructor(name,salary) values ('Arjun',500000);
insert into Instructor(name,salary) values ('Sameer',65000);
insert into Enrollment(course,std_id) values ('Java',101);
insert into Enrollment(course,std_id) values ('Python',102);
insert into Enrollment(course,std_id) values ('Java',105);
insert into Enrollment(course,std_id) values ('Java',107);
select upper(substr(name,-3,3)),job from Student;
select * from Student where dept='CSE';
select * from Employee where name in ('Smith','Allen');
select count(*) from Student where dept='CSE';
select dept,count(*) from Student group by dept;
select * from Student where roll not in (select std_id from Enrollment);
select * from Student where dept in ('CSE','AIML');
select deptno,dept,count(*) as no_of_stds from Student group by deptno,dept order by
no_of_stds desc fetch first 1 row only;
select distinct course from Enrollment;
select * from Student where address='Gandipet';
select * from Employee where name like 'S_____';
select * from Instructor where salary>10000;
Output: