0% found this document useful (0 votes)
48 views7 pages

Database All Queries of MID TERM

Uploaded by

tarekms008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views7 pages

Database All Queries of MID TERM

Uploaded by

tarekms008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Course Name: Introduction to DATABASE

Topic: QUERY WRITING

By:

SADAT JUBAYER

AIUB COURSE SOLUTION | ACS


AIUB COURSE SOLUTION

Lab Performance 01
1. Create a query to display the name and salary of employees earning more than $2850.

SELECT ename,sal FROM emp WHERE sal>2850;

2. Create a query to display the employee name and department number for employee number 7566.

SELECT ename,deptno FROM emp WHERE empno=7566;

3. Display the employee name, job, and start date of employees hired between February
20, 1981, and May 1, 1981. Order the query in ascending order by start date.

SELECT ename,job,hiredate FROM emp WHERE hiredate BETWEEN '20-FEB-81' AND '1-MAY-81' ORDER BY
hiredate;

4. Display the employee name and department number of all employees in departments
10 and 30 in alphabetical order by name.

SELECT ename,deptno FROM emp WHERE deptno IN(10,30) ORDER BY ename;

5. Display the name and hire date of every employee who was hired in 1982.

SELECT ename,hiredate FROM emp WHERE hiredate LIKE '%82';

6. Display the name and job title of all employees who do not have a manager.

SELECT ename,job FROM emp WHERE mgr IS NULL;

7. Display the name, salary, and commission for all employees who earn commissions. Sort data in descending
order of salary.

SELECT ename,sal,comm FROM emp WHERE comm IS NOT NULL ORDER BY sal;

8. Display the names of all employees where the third letter of their name is an A.

SELECT ename FROM emp WHERE ename LIKE '__A%';

9. Display the name of all employees who have two Ls in their name.
SELECT ename FROM emp WHERE ename LIKE '%L%L%';

10. Display the losal and hisal columns from the salgrade table.
SELECT losal,hisal FROM salgrade;
AIUB COURSE SOLUTION

Lab Performance 02
1. Display the employee number, hiredate and number of months Blake has worked.

SELECT empno,hiredate, MONTHS_BETWEEN(SYSDATE,hiredate) FROM emp WHERE ename='BLAKE';

2. Display the date 01-SEP-18. Use date function add_months.

SELECT add_months('1-SEP-18',6) FROM dual;

3. Display the employee number, hiredate and rounded hiredate to month. Use the round function.

SELECT ename, hiredate, ROUND(hiredate,'month') FROM emp;

4. Display the employee number, hiredate and truncated hiredate to month. Use the trunc function.

SELECT empno,hiredate, TRUNC(hiredate,'month') FROM emp;

5. Manipulate the salary of employee Blake in such a way that it is displayed as $002,850.

SELECT to_char(sal,'$99,999') FROM emp WHERE ename='BLAKE';

6. Display the hiredate of all employees in the format DD-MONTH-YEAR(e.g 17 December Nineteen
Eighty).

SELECT to_char((hiredate),'DD MONTH YEAR') FROM emp;

7. Display Today’s date and time.

SELECT SYSDATE,TO_CHAR(SYSDATE,'HH24:HH:MM:SS') FROM DUAL;

8. Display all the data of commission column in such a way that if there is any null value in this column it
will be replaced by the string ‘No Commission Yet’.
SELECT NVL(TO_CHAR(COMM),'No Comission Yet') FROM emp;

9. Check if the values of column ename and job are equal using nullif function.

SELECT NULLIF(ename,job) FROM emp;


AIUB COURSE SOLUTION

10. Manipulate the data of comm column of emp table in such way that if data is null it will display NULL
and if data is not null it will display not null.

SELECT NVL2(to_char(comm),'NOT NULL','NULL') FROM emp;

11. Display the incremented salary of salesman by $2000, manager by $3000 and president by $4000.
Salaries of other employee will not increase. Use decode function.

SELECT DECODE(job,'SALESMAN',sal+2000,'MANAGER',sal+300,'PRESIDENT',sal+4000,sal) FROM


emp;

12. Write a query to display the employee names in lowercase letter.

SELECT LOWER(ename) FROM emp;

13. Display the employee name and department number by joining the columns using concatenation
function.

SELECT ename||deptno FROM emp;

14. Calculate and display the rounded salary of employee King and Ford after dividing salary by 300.

SELECT round(sal/300) FROM emp WHERE ename IN ('KING','FORD');

15. Calculate and display the remainder of the ratio of salary to commission for all employees whose job
title is salesman.

SELECT MOD(sal,comm) FROM emp WHERE job='SALESMAN';


AIUB COURSE SOLUTION

Lab Performance 03
1. Find the average, minimum and maximum salary of the employees. Label the columns AVG, MIN and MAX
respectively.

SELECT AVG(sal) "AVG", MIN(sal) "MIN", MAX(sal) "MAX" FROM emp;

2. Modify your previous query to find the average, minimum and maximum salary of employees according to
department number.

SELECT deptno,AVG(sal) "AVG", MIN(sal) "MIN", MAX(sal) "MAX" FROM emp GROUP BY deptno;

3. Modify your previous query to find the average, minimum and maximum salary of employees according to job
category.

SELECT job,AVG(sal) "AVG", MIN(sal) "MIN", MAX(sal) "MAX" FROM emp GROUP BY job;

4. Display the department number and minimum salary grouped by department number but make sure the
minimum salary is greater than $800.

SELECT deptno,MIN(sal) FROM emp GROUP BY deptno HAVING MIN(sal)>800;

5. Display the number of employees whose job is salesman.

SELECT COUNT(job) FROM emp WHERE job='SALESMAN';

6. Display the sum of salaries grouped by the job of employees.

SELECT SUM(sal) FROM emp GROUP BY job;

7. Display the sum of salaries grouped by the department number.

SELECT SUM(sal) FROM emp GROUP BY deptno;

8. Display the sum of salaries grouped by the department number and job but the sum of salary must be greater
than 5000.

SELECT SUM(sal) FROM emp GROUP BY deptno,job HAVING SUM(sal)>5000;

9. Display the number of employees whose department number is 20.

SELECT COUNT(deptno) FROM emp WHERE deptno=20;


AIUB COURSE SOLUTION

Lab Quiz Midterm


1. Display the job and the hire date of the most senior employee of each department and order the result by
ascending order.

SELECT job, hiredate FROM emp ORDER BY hiredate;


2. Display the modulus of salary and commission considering also the null values. Rename the column
Remainder Value.

SELECT MOD(sal,NVL(comm,0)) as "Remainder value" FROM emp;


3. Display the employee number, employee name, job, salary and start date of employees
hired between February 20, 1981, and May 1, 1981. Order the query in descending order by hire date.
SELECT empno,ename,job,sal,hiredate FROM emp WHERE hiredate BETWEEN('20-FEB-1981') AND
('01-MAY-1981') ORDER by hiredate DESC;
4. Write a query to display the employee name, department number and salary for all employees.

SELECT ename,deptno,sal FROM emp;


5. Display the job and the hire date of the most junior employee of each department and order the result by
descending order.

SELECT deptno,job,max(hiredate) FROM emp GROUP BY deptno,job ORDER BY deptno DESC;


6. Display today’s day, date and time. The format must return a result like this:
Thursday, 13th July 17, 2:45:55.Label the column as Day Date Time.

SELECT TO_CHAR(SYSDATE,'DAY-DDTH-MON-YY HH24:MI:SS') "NOW" FROM dual;

7. Display the name, salary and revised Salary. If manager ID is 7902 revised salary will be actual
salary+100, if manager ID is 7788 revised salary will be actual salary+200. For others the revised salary
will be actual salary+60. Sort by descending order of revised salary.

SELECT ename, sal from emp (not completed)

8. Display the truncated average remainder of the ratio of losal to hisal considering also the null values.
Rename the column Remainder.

SELECT ename, avg(sal+comm) AS Remainder FROM emp;


AIUB COURSE SOLUTION

9. Display the employee number, employee name, job, salary and start date of employees whose salary is
between 1000 and 2000. Order the query in ascending order by salary.

SELECT empno, ename,job,sal,hiredate FROM emp WHERE sal BETWEEN 1000 AND 2000 ORDER BY
SAL;

10. Display the name and job of all employees who have the letters ‘ERK’ or ‘LES’ in their designation.
Make sure there is no other letters in between the said letters.

SELECT ename, job FROM emp WHERE job LIKE '%ERK%' OR job LIKE '%LES%';

Don’t meorize Queries!

You might also like