Hello Friends , today we will be creating and use the Employee Database Scheme to answer the given queries !
Here are the necessary details for the overall database scheme
Employee Scheme
Field | Type | NULL | KEY | DEFAULT |
---|---|---|---|---|
Eno | Char(3) | NO | PRI | NIL |
Ename | varchar(50) | NO | NIL | |
Job_type | varchar(50) | NO | NIL | |
Manager | Char(3) | Yes | FK | NIL |
Hire_date | Date | NO | NIL | |
Dno | int | YES | FK | NIL |
Commission | Decimal(10,2) | YES | NIL | |
Salary | Decimal(7,2) | NO | NIL |
Employee State
Eno | Ename | Job_Type | Manager | Hire_Date | Dno | Commission | Salary |
---|---|---|---|---|---|---|---|
765 | Martin | Sales_man | 198 | 1981-04-22 | 30 | 1400 | 1250 |
756 | Jones | Manager | 783 | 1981-04-02 | 20 | 0 | 2300 |
752 | Ward | Sales_man | 769 | 1981-02-22 | 30 | 500 | 1300 |
749 | Allan | Sales_man | 769 | 1981-02-20 | 30 | 300 | 2000 |
736 | Smith | Clerk | 790 | 1980-12-17 | 20 | 0 | 1000 |
793 | Miller | Clerk | 788 | 1982-01-23 | 40 | 0 | 1300 |
792 | Ford | Analyst | 756 | 1981-12-03 | 20 | 0 | 2600 |
790 | James | Clerk | 769 | 1981-12-03 | 30 | 0 | 950 |
787 | Adams | Clerk | 778 | 1983-01-12 | 20 | 0 | 1150 |
784 | Turner | Sales_man | 769 | 1981-09-08 | 30 | 0 | 1450 |
783 | King | President | NULL | 1981-11-17 | 10 | 0 | 2950 |
788 | Scott | Analyst | 756 | 1982-12-09 | 20 | 0 | 2850 |
778 | Clark | Manager | 783 | 1981-06-09 | 10 | 0 | 2900 |
769 | Blake | Manager | 783 | 1981-05-01 | 30 | 0 | 2870 |
Department Scheme
Field | Type | NULL | KEY | DEFAULT |
---|---|---|---|---|
Dno | int | No | PRI | NULL |
Dname | Varchar(50) | Yes | NULL | |
location | Varchar(50) | Yes | New Delhi |
Department State
Dno | Dname | Location |
---|---|---|
10 | Accounting | New York |
20 | Research | Dallas |
30 | Sales | Chicago |
40 | Operation | Boston |
50 | Marketing | New Delhi |
Please Note : We have used Oracle 11g for implementing all our queries and also recommend you to do so for better understanding !
Now we will answer the given queries
QUERIES
1) Query to display Employee Name, Job, Hire Date, Employee Number; for each employee with the Employee Number appearing first.
select eno,ename,job_type,hire_date from employee
2) Query to display Unique Jobs from the Employee Table.
select DISTINCT(job_type) From Employee
3) Query to display the Employee Name concatenated by a Job separated by a comma.
select ename || ' , ' || job_type from employee
4) Query to display all the data from the Employee Table. Separate each Column by a comma and name the said column as THE_OUTPUT.
select eno || ' , ' || ename || ' , ' || job_type || ' , ' || manager || ' , ' || hire_date || ' , ' || dno || ' , ' || commission || ' , ' || salary as THE_OUTPUT from employee
5) Query to display the Employee Name & Salary of all the employees earning more
than $2850.
select ename,salary from employee where salary>'2850';
6) Query to display Employee Name & Department Number for the Employee No= 7900.
select ename,dno from employee where eno='7900'
7) Query to display Employee Name & Salary for all employees whose salary is not in
the range of $1500 and $2850.
select ename,salary from employee where salary<1500 and 2850 >salary or select ename,salary from employee where NOT EXISTS (select salary from employee where salary <1500 AND 2850>salary ) or select ename,salary from employee where NOT EXISTS (select salary from employee where salary BETWEEN '1500' AND '2850' )
8) Query to display Employee Name, Job, and Hire Date of all the employees hired between Feb 20, 1981 and May 1, 1981. Order the query in ascending order of Start Date.
select ename,job_type,hire_date from employee where hire_date BETWEEN '20-Feb-1981' AND '1-May-1981' order by hire_date
9) Query to display Employee Name & Department No. of all the employees in Dept 10
and Dept 30 in the alphabetical order by name.
select ename,dno from employee where dno in(10,30) order by ename
10) Query to display Employee Name & Salary of employees who earned more than
$1500 and are in Department 10 or 30.
select ename,salary from employee where salary>1500 AND ((DNO=10)OR (DNO=30))
11) Query to display Name & Hire Date of every Employee who was hired in 1981.
Select Ename,HIRE_DATE from EMPLOYEE Where HIRE_Date like ‘%81’
12) Query to display Name & Job of all employees who don’t have a current Manager.
SELECT ENAME,JOB_TYPE FROM EMPLOYEE WHERE NOT (JOB_TYPE='MANAGER')
13) Query to display the Name, Salary & Commission for all the employees who earn commission. Sort the data in descending order of Salary and Commission.
SELECT ENAME,salary,commission from employee where NOT (commission=0) ORDER BY SALARY,COMMISSION DESC
14) Query to display Name of all the employees where the third letter of their name is ‘A’.
SELECT ENAME FROM EMPLOYEE WHERE ENAME LIKE '__a%'
15) Query to display Name of all employees either have two ‘R’s or have two ‘A’s in their name & are either in Dept No = 30 or their Manger’s Employee No = 7788.
SELECT ENAME FROM EMPLOYEE WHERE (ENAME LIKE '%R%r%' OR ENAME LIKE '%A%a%') AND (DNO='30' OR MANAGER='7788')
16) Query to display Name, Job and Salary of all employees whose Job is Clerical or
Analyst & their salaries are not equal to 1000, 3000, or 5000.
SELECT ENAME,JOB_TYPE,SALARY FROM EMPLOYEE WHERE JOB_TYPE IN('CLERK','ANALYST') AND SALARY NOT IN (1000,3000,5000)
17) Query to display Name, Salary and Commission for all employees whose
Commission Amount is greater than their Salary increased by 5 %.
SELECT ENAME,SALARY,COMMISSION FROM EMPLOYEE WHERE COMMISSION > (SALARY*0.05)
18) Query to display the Current Date.
SELECT Sysdate FROM Dual