SQL Practical Questions with Answers | SQL Practice with Solutions

1. Create the following table, “students” and insert the data into the table.

roll_nonameaddressbirth_date
1LizaTinsukia12-02-2002
2PritomDibrugarh10-03-2000
3ArjinaTinsukia11-02-2001
4MonicaJorhat03-04-1995
5AlijaGuwahati01-01-1995
6AmishaTinsukia05-02-2011
7KishorJorhat01-02-2002
8AnujTinsukia02-01-1998
9BarshaDibrugarh06-03-1996
10PahiTinsukia06-02-2012

Write SQL commands for the following on the basis of above table.

(i) Display all records of students from the above table.

(ii) Retrieve all the data of the students.

(iii) Display roll number and name of students.

(iv) Display the details of students who are from ‘Tinsukia’.

(v) List the name and birth date of students in Dibrugarh.

(vi) Delete all the records from the table where address has the value ‘Jorhat’.

(vii) Change the address of roll number 9 from Dibrugarh to Guwahati.

(viii) Change the value of birth date column to ‘5-3-2012’ where the name column has the value ‘Amisha’.

(ix) Change “Guwahati” to “Jorhat” in the address column in students table.

(x) Delete all the data from the students table.

Answer:

2. Write SQL commands for the following on the basis of given table STUDENT.

StudentNoClassNameGameGrade1SUPWGrade2
111RahulCricketBPhotographyA
212RajeshTennisAGardeningC
310BipulSwimmingBPhotographyB
411PriyaTennisCCookingA
512AakashBasketballALiteratureA
610PratimaCricketAGardeningC
SUPW: Socially Useful Productive Work

(i) Display all the student’s records from the table.

(ii) Display the records of students who are studying in class 12.

(iii) Display the name of the students who plays game tennis.

(iv) Display the StudentNo and name of those students who does not play cricket.

(v) Display the name, grade1 and grade2 of the students who are getting grade ‘B’ in either Game or SUPW or both

(vi) Display the name, grade1 and grade2 of the students who are getting grade ‘A’ in both Game and SUPW.

(vii) Display the different games offered in the school.

(viii) Display the name and SUPW of the students whose name starts with ‘P’.

(ix) Display the StudentNo and name of those students who have got same grade in Grade1 and Grade2.

(x) Display all information of students in the alphabetic order of their name.

Answer:

3. Write SQL commands for the following on the basis of table SPORTS.

StudentNoClassNameGame1Grade1Game2Grade2
15RohitCricketBSwimmingA
26DebojitTennisASkatingC
35ArmanSwimmingBFootballB
48ManishaTennisCTennisA
59DhirajBasketballACricketA
610IshaCricketAAthleticsC

(i) Display the name, grade1 and grade2 of those students who are getting grade ‘C’ in either Game1 or Game2 or both

(ii) Display the name, game1 and game2 of those students who have same game for both game1 and game2.

(iii) Display the name, grade1 and grade2 of the students who are getting grade ‘B’ in both Game1 and Game2.

(iv) Display the names and games (Game1, Game2) taken by the students whose name starts with ‘D’.

(v) Display the name and class of those students whose name ends with ‘a’. Or, display the name and class of students whose name contains ‘a’ as the last alphabet.

(vi) Display the name of the students having ‘h’ as the third character. Or, display the name of students whose name contains ‘h’ as the third alphabet

(vii) Display the name of the students whose name contains ‘D’ as first alphabet and ‘i’ as third alphabet.

(viii) Write an SQL query to list out the students whose name starts with ‘D’ and ends with ‘t’.

(ix) List out the students whose name length is 4 and starts with ‘I’.

(x) List the names of the students whose name has only 5 characters and starts with ‘A’ and ending with ‘n’.

(xi) Write an SQL query to find those students whose name ends with ‘a’ and six character long.

(xii) Display the names of the students whose name starts either with ‘M’ or with ‘D’.

(xiii) List the students whose names containing the letter ‘a’.

Answer:

4. Create the following table ‘Employee’ and insert the data into the table. Based on the table, solve the following queries.

empID ( Primary Key)empNameAddressAgeSalary
101ArpanTinsukia2020000
102RubiTinsukia1918000
103DipannitaDibrugarh2022000
104VidyaTinsukia2021000
105JirushDibrugarh2125000
106DebojitTinsukia2524000
107DipsikhaJorhat2825000
108GaneshTinsukia2635000
109LakhyaDibrugarh3045000
110LaxmiGuwahati2015000

(i) Write the SELECT command to display the details of the employee with empid as 104.

(ii) Write the SELECT command to display all the records of those employees who are getting salary more than 30000.

(iii) Write the SELECT command to display the employee’s records who are getting salary less than 20000.

(iv) List out the employees who are earning salary between 35000 and 45000.

(v) Display the details of employees who are from Tinsukia or Dibrugarh.

(vi) Display the employee ID and name of employees using alias name “Employee ID”, “Employee Name”.

(vii) Display the details of employees who are not from Tinsukia or Dibrugarh.

(viii) List out the employees who are from Tinsukia and draw the salaries between more than 25000.

(ix) Write a SELECT command to display id, name and age of the employees with their age in ascending order.

(x) List out the employees id, name and salary in descending order based on salary column.

Answer:

5. Write SQL commands for the following on the basis of the given table EMPLOYEES.

EmpID (Primary Key)EmpNameCityJoiningDatePositionSalary
1BishalTinsukia12/08/2020Manager50000
2ChandanDibrugarh10/08/2012Sr. Eng.65000
3AniketDuliajan15/05/2016Jr. Eng.55000
4UtpalDibrugarh25/07/2018Clerk30000
5KunalTinsukia28/10/2019Cashier40000
6ShrutiJorhat18/10/2023Clerk30000

(i) Display employee ID, employee name and joining date of employees .

(ii) Fetch details of employees with city column value as ‘Tinsukia’.

(iii) Change the value of salary column to 40000 where the position column has the value ‘clerk’.

(iv) Change the table name Employees to EmployeeDetails.

(v) Select all the records where the value of name starts with ‘A’.

(vi) Select all the records where the value of salary is in range 40000 and 60000.

(vii) Display the total salaries payable to employees.

(viii) Select highest salary from the table.

(ix) Add three columns to the table as i) TA  ii) DA   and iii) Total

(x) Set the value of TA as 15% of the salary, DA as 12% of the salary and total be sum of salary, TA and DA

Answer:

6. Write SQL commands for the following on the basis of table CLUB.

Coach_IDCoachNameAgeSportsPayGenderDateOfApp
1Ankit25Karate15000M2020-03-27
2Arjun26Karate12000M2022-02-25
3Bipon25Squash17000M2020-02-25
4Pratima22Basketball16000F2022-03-22
5Yash24Swimming20000M2022-01-12
6Rajiv23Swimming20000M2023-01-11
7Vinie22Squash16000F2024-01-01
8Neelam23Karate16000F2024-03-01
9Prachi20Swimming15000F2023-03-03
10Raja25Basketball18000M2023-05-03

(i) Show all information about the swimming coaches in the club.

(ii) Display the different sports offered in the club.

(iii) List the name of all coaches with their date of appointment. Date of appointment is in descending order.

(iv) Display a report showing coach name, age, pay and bonus (15% of pay) of all the coaches.

(v) Display first 5 records of coaches from the table-CLUB.

(vi) Display first 5 records of Male coaches from the table-CLUB.

(vii) Display 3 records starting from 5th records from the table-CLUB.

(viii) Display 5 records skipping the first 2 records.

(ix) Display details of coaches from records 4 to 7.

(x) Display the records of top 5 coaches who are getting highest payment.

(xi) Display the records of 5 coaches who are getting lowest payment.

(xii) Display the records coaches who are getting 2nd highest payment.

(xiii) Display the records coaches who are getting 3rd highest payment.

Answer:

7. Write SQL commands for the following on the basis of the table employee.

EmpIDNameSalary
100Arun Saha50000
101Rishiraj Bhattacharjee40000
102Subham Kr. Singh30000
103Manas Pratim Bhowmick35000
104Swapnil Biswas18000
105Himanjal Gogoi22000
106Rohit Kewat20000
107Sashank Kr. Singh25000

(i) Write a query to get the total salaries payable to employees.

(ii) Write a query to find the name of the employee who earn the maximum salary. Return employee name and maximum salary.

(iii) Write a query to get the name of the employee who draws minimum salary. Display name of employee and minimum salary.

(iv) Write a query to get the average salary payable to employees and number of employees in the employee table.

(v) Write a query to get the highest, lowest, sum and average salary of all employees.

(vi) Write a query to get the number of employees working with the company.

(vii) Find how many employees getting salary more than or equal to 40000.

(viii) Display the difference between the highest and lowest salaries of employees.

(ix) Add four columns to the table Employee as TA, DA, HRA and Total.

(x) Set the value TA as 15% of the salary, DA as 10% of the salary, HRA as 20% of the salary and total be sum of salary, TA, DA and HRA.

Answer:

8. Create the following table EmpDetail and insert the data into the table.

EmpIDFNameLNamePhNoHireDateJobIDSalaryDeptID
1SamuelDhan90457982312020-06-10IT-Prog30000101
2RajaGogoi75457982302022-06-10WebDev25000102
3RajivKumar83457982322020-06-10HRM35000103
4PratimaSharma92457982302020-06-10WebDev25000102
5YashSwami70457982342023-06-10IT-Prog30000101
6SulagnaSarkar80457982352021-06-10Sales18000104

(i) Write a query to display the names (FName, LName) using alias name “First Name”, “Last Name”.

(ii) Write a query to get unique department id from the EmpDetail table.

(iii) Write a query to get all employee details, display first name in descending order.

(iv) Write a query to get the names (first name, last name), salary, PF of all the employees. (PF is calculated as 12% of salary)

(v) Write a query to get the employee id, names (FName, LName) and salary from the EmpDetail table, salary would be in ascending order.

(vi) Write a query to get the total salary payable to Web Devloper.

(vii) Write a query to get the number of employees working with the company who is working as ‘IT_Prog’.

(viii) Write a query to get the number of jobs available in the EmpDetail table.

(ix) Write a query to get all first name from EmpDetail table in Upper case.

(x) Write a query to get the first three characters of first name from EmpDetail table.

Answer:

9. Write SQL commands for the following on the basis of ‘marks’ table.

RollNoSubjectMarks
1English90
2English60
3English80
1Math80
2Math80
3Math65
1Science70
2Science50
3Science85

(i) Display roll number and total marks of each student from marks table.

(ii) Display the average marks of each student.

(iii) Display roll number, subject and maximum marks of each student.

(iv) Display roll number subject and minimum marks of each student

(v) Display roll number and total marks of each student where total marks is less than 200.

(vi) Find the highest marks of each subject. Return subject and highest marks.

(vii) Display roll number and percentage of each student.

(viii) Display roll number and total marks of each student and sort it on the basis of total marks.

(ix) Display roll number and the number of subjects enrolled by each student.

(x) Display the subjects with maximum marks of each student where maximum marks is greater than 80.

Answer:

10. Create a table ‘employee’, insert data and write SQL commands for the following.

emp_iddept_idnamecitysalary
1104SajidGuwahati70000
2104AshishDibrugarh40000
3101SweetyTinsukia50000
4103ArjunGuwahati55000
5101DhirajDibrugarh75000
6104BishalDibrugarh60000
7103RohitJorhat55000
8102ShrutiDibrugarh50000
9101GaneshTinsukia55000
10102NitulTinsukia40000

(i) Display the dept_id, total, average, maximum and minimum salary for each department’s employees.

(ii) Display dept_id, maximum salary for each department with “dept_id” greater than 102.

(iii) Display the city and number of employees from each city.

(iv) Display the number of employees who are earning salary greater than 55000.

(v) Write a query to get the employee name and maximum salary who are from Tinsukia City.

Answer:

11. Create table EmpData and insert the data in the table.

emp_idemp_nameworking_dateworking_hours
1Kunal2020-01-2010
2Arnav2020-01-208
3Rahul2020-01-2010
4Binoy2020-01-206
1Kunal2020-01-2112
2Arnav2020-01-2110
4Binoy2020-01-218
3Rahul2020-01-216
1Kunal2020-01-228
2Arnav2020-01-2210

(i) Display employee id, sum and average working hours grouped on the employee id.

(ii) Display employee name, total working hours of each employee and sort it in the descending order of the sum of working hours.

(iii) Display employee name and total working hours of each employee, where total working hours is greater than 20.

(iv) Display only the name of employees whose maximum working hours is greater than or equal to 10.

(v) Write a SQL query to find the highest working hours of each employee. Return employee name and highest working hours.

Answer:

12. Create the following table ‘Customer’ and insert data into the table.

cust_idcust_namecitygrade
1002Dipannita PaulTinsukia100
1007Rishi DasTinsukia200
1005Priya BaruahSivasagar300
1008Amit KumarDibrugarh200
1004Sajid AhmedGuwahati400
1009Priya SharmaDibrugarh500
1003Yashmin AhmedJorhat100
1001Rubi DuttaTinsukia200
1006Minakshi PhukonSivasagar300

(i) Display the customer’s name and city in uppercase letters.

(ii) Display the name of employees with name starting with the letter R.

(iii) Find the highest and lowest grade of the customers in each city. Display city, maximum grade and minimum grade.

(iv) Display only those cities whose maximum grade is greater than 300.

(v) Display the name of cities where city count is greater than or equal to 2.

Answer:

13. Create the table “orders” and add the records in the table.

ord_nopur_amtord_datecust_id
1001150020/10/2020103
1009250010/09/2020101
1002650020/10/2020102
1004110017/08/2020101
1007950010/09/2020103
1005150027/07/2020104
1008202010/09/2020102
1010300010/10/2020104
1003180010/10/2020101

(i) Display ord_no and ord_date with column heading, “Order Number” and “Order Date”.

(ii) Find the number of orders given by each customer. Display customer id and number of orders.

(iii) Display customer id and the highest purchase amount ordered by each customer.

(iv) Show the customer id and total purchase amount of each customer. Total purchase amount should be in ascending order.

(v) Display order date, sum and average purchase amount grouped on the order date.

Answer:

14. Answer the following:

(i) Create a table “EMP” with the following columns:

EMP_NO, EMP_NAME, JOIN_DATE, JOIN_BASIC

(ii) Insert the data:

EMP_NOEMP_NAMEJOIN_DATEJOIN_BASIC
1001Rishi Das01-JUN-202230000
1002Bibek Das01-JUN-202225000
1003Kunal Karmakar01-JUN-202230000
1004Arnav Dutta01-JUL-202225000
1005Roktim Gogoi01-JUL-202228000

(iii) Display employee number and employee name from the above table with column heading “Employee Number” and “Employee Name”.

(iv) Display the name of employees with names starting with the letter, R.

(v) To the table EMP, add a new column, DESIG varchar (3).

(vi) Update the table and add data to DESIG column.

EMP_NODESIG
1001EXE
1002DIR
1003DIR
1004MGR
1005MGR

(vii) Display the different designation of EMP table.

(viii) Retrieve the records of employees whose designation is not DIR or MGR.

(ix) List out the employees whose designation is “MGR” and getting join basic more than 25000.

(x) List out the designation wise maximum join basic of the employees. Return employee name, designation and maximum join basic.

Answer:

15. Answer the following-

    (i) Create a table “Salary” with the following columns:

    EMP_NO, BASIC_SALARY, COMMISSION, DEDUCTION, SALARY_DATE

    (ii) Insert the following data into the “Salary” table.

    EMP_NOBASIC_SALARYCOMMISSIONDEDUCTIONSALARY_DATE
    1001300002000250030-JUN-2024
    1002250001200200030-JUN-2024
    1003300005000290030-JUN-2024
    1004250002000300030-JUN-2024
    1005280001000250030-JUN-2024
    1001300002000250031-JUL-2024
    1002250001200200031-JUL-2024
    1003300005000290031-JUL-2024
    1004250002000300031-JUL-2024
    1005280001000250031-JUL-2024

    (iii) Display emp_no, basic salary, commission, deduction and net salary, sort net salary in descending order.

    NET SALARY=BASIC_SALARY+COMMISSION-DEDUCTION

    (iv) Display the basic salary and HRA, HRA is calculated as 10% of basic salary.

    (v) Display the emp_no, basic salary and commission of employees whose basic salary + commission is greater than 30000.

    (vi) Display total basic salary of each employe and sort it in the descending order of total of basic salary.

    (vii) Display the employee number of those employees whose average basic is greater than 20000.

    (viii) To the table Salary, add a new column, DEPARTMENT.

    (viii) Update the table with department set to SLS, ACC, MKT

    EMP_NODEPARTMENT
    1001ACC
    1002SLS
    1003SLS
    1004MKT
    1005MKT
    (x) Display maximum, minimum, average and sum of basic of each department.

    Answer:

    2 thoughts on “SQL Practical Questions with Answers | SQL Practice with Solutions”

    Leave a Comment