# SQL practice questions set - 2

2,914 views

Published on

SQL practice questions

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
2,914
On SlideShare
0
From Embeds
0
Number of Embeds
28
Actions
Shares
0
150
0
Likes
0
Embeds 0
No embeds

No notes for slide

### SQL practice questions set - 2

1. 1. 1. Display the highest,lowest,sum and average salaries of all employees. Label the columns as Maximum, Minimum, Sum, Average respectively. Round your results to the nearest whole number. 2. Determine the number of managers without listing them. Label the column Number of Managers. 3. Write a query to display the difference between the highest and lowest salaries. Label the column DIFFERENCE. 4. Display the manager number and the salary of the lowest paid employee for that manager. Exclude any one whose manager is not known. Exclude any groups whose where the maximum salary is 6,000 or less. 5. Write a query to extract the year from hire_date column of the EMPLOYEES table for those employees who work in department 80. 6. Display the highest,lowest,sum and average salaries of all employees for each job type. Label the columns as Maximum, Minimum, Sum, Average respectively. Round your results to the nearest whole number. 7. Write a query to display the number of people with the same job. 8. Write a query to display each department name, location number, number of employees and the average salary of all employees in that department. Label the columns Name, Location, Number of People and Salary respectively. Round the average to two decimal places. 9. Create an Emp table with the following criteria: Eno number(6) Ename varchar2(30) Sal number(8,2) deptno number(4) 10. Add a table – level primary key constraint to the EMP table on the ID column. The constraint should be named at creation. Name the constraint my_emp_id_pk. 11. Create a primary key constraint to the DEPT table using the ID column. The constraint should be named at the time of creation. Name the constraint my_dept_id_pk. 12. Add a column dept_id to the EMP table. Add a foreign key reference on the EMP table that ensures that the employee is not assigned to a non-existent department. Name the constraint my_emp_dept_id_fk. 13. Modify the EMP table. Add a commission column of number datatype, precision 2, scale 2. Add a constraint to the commission column that ensures that a commission value is greater than zero. 14. Remove the table Emp from the database 15. Clear the recyclebin 16. Display the highest,lowest,sum and average salaries of all employees for each job type. Label the columns as Maximum, Minimum, Sum, Average respectively. 17. Write a query to display the number of people with the same job. 18. Write a query to display each department name, location number, number of employees and the average salary of all employees in that department. Label the columns Name, Location, Number of People and Salary respectively. Round the average to two decimal places. 19. Create a table student_marks with the following conditions (a) Roll number with the data type number of size 3 and should not have null values (b) Year with the data type number of size 1 and should not have null values (c) Marks with the data type number of size 5,2 and should not have null values (d) Remarks with the data type varchar2 of size 20 The roll number should be between 1 and 250 , year should be between 1 and 3 , marks should be between 0 and 100. Give appropriate constraint names. 20. Create a table student with the following conditions (a) Roll number with the data type number of size 3 (b) City with the data type varchar2 of size 20
2. 2. (c) Name with the data type varchar2 of size 30 None of the column should have a null value. The roll number should be with the range of 1 to 250. The length of the city should be 3 and all the characters should be in uppercase. First character of the name should be in capital letters. Give appropriate constraint names. 21. Create a table stud with the following conditions (a) Roll number with the data type number of size 3 (b) Name with the data type varchar2 of size 30 (c) Year with the data type date (d) Branch with the data type varchar2 of size 20 (e) EN with the data type number of size 4 Define an unique on the column EN. Define a composite primary key on the columns Roll number,year and branch. Give appropriate constraint names. 22. Create a table supplier with the following conditions (a) Supplier_Id with the data type number of size 10 (b) Supplier_name with the data type varchar2 of size 30 (c) Contact_name with the data type varchar2 of size 30 Define a primary key on Supplier_Id. Supplier_Name should not contain null values. 23. Create a table products with the following conditions: (a) Product_Id with the data type number of size 10 (b) Sup_Id with the data type number of size 10 Refer Supplier_Id of supplier table to Sup_Id of products table. Give appropriate constraint names. 24. Create a table product with the following conditions (a) Product_Id with the data type number of size 5 (b) Product_name with the data type varchar2 of size 20 (c) Supplier_name with the data type varchar2 of size 20 (d) Unit_Price with the data type number of size 10 Define priamry key on Product_Id. Product name and Supplier name should not be null. Unit Price should be more than 5000. 25. Create a table order_items with the following conditions (a) Order_Id with the data type number of size 5 (b) Product_Id with the data type number of size 5 Define priamry key on Order Id and foreign key on Product_Id reference product table product_id. Give appropriate constraint names. 26. Enter 5 records into each of the tables student_marks, student, stud, supplier, products, product, order_items and produce one error if violating the rules. 27. Remove all the constraints created above from the database. 28. Remove the tables student_marks,student,stud,supplier,products,product,order_items from the database. 29. Clear the recycle bin. 30. Create a view to list all the details of employee table. 31. Create a view to list all the employees whose manager id is 100. 32. Create a view to list employee id , employee name , department id , department name and designation 33. Create a view that gives information about department name,its average salary, maximum salary and minimum salary. 34. Write a query to retrieve the data from view created above in Q12. 35. Write a query to retrieve the data from view created above in Q13. 36. Write a query to retrieve the data from view created above in Q14. 37. Write a query to retrieve the data from view created above in Q15 38. Drop all the views created above.
3. 3. 39. Create a default sequence. 40. Create a sequence with the following credentials (1) incremented by 2 (2) start with 10 (3) minimum value must be 1 (4) maximum value must be 1000 (5) buffer size should be 5 41. Write a query to select the next value and current value from the above sequence. 42. Create a table with the name stud_marks(sid,sname,city). 43. Insert the values into the above table for sid column, using sequence created in Q23 and rest other column values must be user defined. Enter 10 records. 44. Write a query to display the last name,department number and department name for all employees. 45. Write a query to display the employee last name , department name, location id, and city of all employees who earn a commission. 46. Create a unique listing of all jobs that are in department 80. Include the location of the department in the output. 47. Write a query to display the employee last name , department name, location id, and city of all employees who earn a commission. 48. Write a query that displays the last name, job, department name, salary and grade for all employees. 49. Write a query to display the name and hire date of any employee hired after employee Davies. 50. Display the employee's last name and department name for all employees who have an 'a'. 51. Create a query that displays the employee last names, department numbers, and all the employees who work in the same department as a given employee. 52. Create a query that displays the last name, job, department name, salary and grade for all employees. 53. Write a query to display Employee id , name , salary , department id , department name , manager id though there are employees existing in a department or not. 54. Write a query to display the details of employees whose designation is same as designation of the employee of "Shipping" department. 55. Write an anonymous block to print the phrase “My First PL/SQL Program”. 56. Write an Anonymous Block to add two numbers and display the o/p 57. Write an Anonymous Block to accept two different number and find addition, subtraction, multiplication and division of given two numbers. 58. Write an Anonymous Block to accept Empno, Empname, Sal, DOB, DOJ at runtime. Display all the details along with age of the employee, experience of employee, annual salary, daily salary as per the current month and also display all the allowances TA, DA, HRA and gross salary. ( TA = 10% of salary , DA = 15% of salary , HRA = 20% of salary) 59. Write an Anonymous Block to accept three different numbers and find the smallest number. 60. Write an Anonymous Block to accept 2 different date values and find the most recent date from the given two. 61. Write an Anonymous Block to accept the product_id, product_desc, quantity and unit price. Now calculate the bill. If the bill more than 500 and less than 1500 give a discount of 5%. If bill amount is more than 1500 then 10% discount. Otherwise there is no discount. Display all the details along with bill amount, discount, net payable.(Bill = quantity * unit price , net payable = bill amount - discount) 62. Write an Anonymous Block to accept three different numbers and find the biggest from three. 63. Write an Anonymous Block to accept Name, Sid, Marks1, Marks2, Marks3. Based on average declare the result. If marks in any one of the subject is less than 35 declare as fail. If avg >=35 and <50 then declare as third class. If avg>=50 and avg<60 then declare as second class. If