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

SQL Problems For Practice

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

SQL Problems For Practice

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

Write SQL query for the below student database. Consider the student database.

Student (regno,name,dept,mark,city,percentage). Give the expression in SQL to express each of the


following queries:
1.Write a query to fetch the number of students studying in the department ‘IT’.
2.Write a query to get the marks >100.

Solution:
1. select count(*) from Student where dept=’IT’;
2. select mark from Student where mark>100;
Write SQL query for the below student database
Consider the student database.
Student (regno,name,dept,mark,city,percentage,mobileno)
Give the expression in SQL to express each of the following queries:
1.Rename student name attribute as sname
2.Write a query to display student name second character as ‘c’
3. Write a query to display the mark range from 70 to 85 of CSE department
4. Write a query to add new column as rollno in student table

Solution:
1. ALTER Table Student
Rename column name to sname;
2. select name from Student where name LIKE ‘_c%’;
3. select mark from Student where dept = ‘CSE’ AND mark BETWEEN(70 AND 85);
4. ALTER Table Student
ADD rollno int;
Write SQL query for the below employee database
Consider the employee database.
Emp(ename,street,city,dept)
Works(ename,companyname,salary)
Company(companyname,city)
Manages(ename,managername)
Give the expression in SQL to express each of the following queries:
1. Write a query to fetch the number of employees working in the department
‘HR’.
2.Write a query to get the salary >60000.
3. Write a query to find all the employees whose salary is between 60000 to
100000.
4.Write a query to find the names of employees that begin with ‘A’

Solution:
1. Select count(*) from Emp where dept =’HR’;
2. Select ename from Works where salary>60000;
3. Select ename from Works where salary BETWEEN 60000 AND 100000;
4. Select ename from Emp where ename LIKE ‘A%’;

Write SQL query for the below student database


Consider the student database.
Student (regno,name,dept,mark,city,percentage,mobileno)
Give the expression in SQL to express each of the following queries:
1. Find the name of all CSE department students with >80% .
2. Find the name of all students who secured more than 70 marks.
3. Find the names of all students living in the salem city and percentage >70.
4. Find the names of all CSE department students living in salem.
Solution:
1. Select name from Student where dept = ‘CSE’ and percentage>80;
2. Select name from Student where mark>70;
3. Select name from Student where city=’salem’ and percentage>70;
4. Select name from Student where dept=’CSE’ and city = ‘salem’;

Write the following queries in SQL


(i) Find the names of employee who have borrowed a book published by McGraw Hill
(ii) Find the names of employees who have borrowed all books published by McGraw-Hill
Assume the databases as –
member(memb_no, name, dob)
books(isbn, title, authors, publisher)
borrowed(memb_no, isbn, date)

Solution:
(i)SELECT name
FROM member
WHERE member.memb_no=borrowed.memb_no
AND books.isbn=borrowed.isbn
AND books.publisher=’McGraw Hill’;

(ii) SELECT distinct M.name FROM Member M,


WHERE NOT EXIST
(
(SELECT isbn FROM books WHERE publisher = ’McGrawHill’
)
EXCEPT(SELECT isbn FROM borrowed R WHERE R.memb_no = M.memb_no
)
);

Create a table: DEPARTMENT (DEPTNO, DNAME, LOC)


Perform the following:
1. Rename the table dept as department.
2. Add a new column PINCODE with not null constraints to the existing table DEPT.
3. Rename the column DNAME to DEPT_NAME in dept table.
4. Change the data type of column LOC as CHAR with size 10.
5. Delete the department table.

Solution:

1. ALTER TABLE dept RENAME TO department;


2. ALTER TABLE department
ADD COLUMN PINCODE INT NOT NULL;
3. ALTER TABLE department
RENAME COLUMN DNAME TO DEPT_NAME;
4. ALTER TABLE department
ALTER COLUMN LOC SET DATA TYPE CHAR(10);
5. DROP TABLE department;

You might also like