Sample Questions And Answers- Self Practice (LBJ Module 1– DBMS & SQL)
Q1: Write a query to display Employee ID, Employee Full Name, Salary, Annual Salary, Job ID , date
on which employee was hired for employees working in department ID 50.
A1:
SELECT Employee_ID, First_Name|| ' ' ||Last_Name "Full Name", Salary, Salary*12 "Annual
Salary", Job_ID, hire_date
FROM Employees
WHERE Department_ID=5;
Q2: Write a query to display unique job ID’s from the Employees Table
A2:
SELECT DISTINCT Job_ID
FROM Employees;
Q3: Create a query to display all the data from Employees table. Separate each column by a comma.
A3:
SELECT Employee_ID || ',' || First_Name || ',' || Last_Name || ',' || Salary || ',' ||
Email_ID || ',' || Job_ID || ',' || Hire_Date || ',' || Department_ID || ',' || Manager_ID
FROM Employees;
Q4: Display employee Id, last name and hire date of every employee who was hired in 2010.
A4:
SELECT Employee_ID,Last_Name, Hire_Date
FROM Employees
WHERE TO_CHAR(Hire_Date,'YYYY') = '2010';
Q5: Display employee Id, last name and hire date of every employee who do not have manager.
A5:
SELECT Employee_ID,Last_Name, Hire_Date
FROM Employees
WHERE Manager_ID is NULL;
Q6: Display employee Id, last name, job ID and hire date of all employees where the third letter of
last name is an -- a.
A6:
SELECT Employee_ID,Last_Name,Job_ID, Hire_Date
FROM Employees
WHERE Last_Name like '__a%';
Q7: Display employee Id, last name, job ID and hire date of all employees where the third letter of
last name is an – a and e. (It’s written a & e but that is not possible so doing for a or e)
A7:
SELECT Employee_ID, Last_Name, Job_ID, Hire_Date
FROM Employees
WHERE Last_Name like '__a%' or Last_Name like '__e%';
Q8: Display employee Id, last name, job ID, Salary and hire date of all employees who works in
department ID 20, 50 and 80.
A8:
SELECT Employee_ID, Last_Name, Job_ID, Hire_Date
FROM Employees
WHERE Department_ID in (20, 50, 80);
Q9: Display employee Id, last name, job ID, Salary and hire date of all employees whose salary ranges
from 5000 to 15000.
A9:
SELECT Employee_ID,Last_Name,Job_ID,Salary, Hire_Date
FROM Employees
WHERE Salary between 5000 and 15000;
Q10: Write a query to display employee Id, last name, Salary, department ID and department name
for all the employees working in department ID 50.
A10:
SELECT Employee_ID, Last_Name,Salary, Department_ID, Department_Name
FROM Employees inner join Departments using (Department_ID)
WHERE Department_ID=50;
Q10: Write a query to display employee Id, last name, Salary, job id, department ID, job title and
department name for all the employees working in department ID 100.
A10:
SELECT e.Employee_ID, e.Last_Name, e.Salary, e.Job_ID, e.Department_ID, j.Job_Title,
d.Department_Name
FROM Employees e inner join Departments d on (e.Department_ID=d.Department_ID) inner join
Jobs j on (e.Job_ID=j.Job_ID)
WHERE e.Department_ID=100;
Q11: Display the name and hire date for all the employees who were hired before their managers,
along with manager’s name and hire date. Label the columns: Employee, EmpHired, Manager,
MgrHired
A11:
SELECT e.First_Name || ' ' || e.Last_Name "Employee" , e.hire_date "EmpHired", m.First_Name ||
' ' || m.Last_Name "Manager" , m.hire_date "MgrHired"
FROM employees m INNER JOIN employees e ON (m.employee_id=e.manager_id)
WHERE m.hire_date>e.hire_date;
Q12: Determine the validity of the following three statements. Highlight either True or False
1. Group functions works across many rows to produce one result per group.
True / False
2. Group functions include nulls in calculations.
True / False
3. The Where Clause restricts rows prior to inclusion in a group calculation.
True / False
Q13: Display Highest, lowest, sum and average salary of all the employees. Label the columns
Maximum, Minimum, Sum and Average respectively.
A13:
SELECT MAX(Salary) "Maximum",MIN(Salary) "Minimum",SUM(Salary) "Sum", AVG(Salary)
"Average"
FROM Employees;
Q14: Display Department ID, Highest, lowest, sum and average salary of all the employees for each
department.
A14:
SELECT Department_ID, MAX(Salary),MIN(Salary), SUM(Salary), AVG(Salary)
FROM Employees
Group by Department_ID;
Q15: Display Job ID, Highest, lowest, sum and average salary of all the employees for each job type
A15:
SELECT Job_ID, MAX(Salary),MIN(Salary), SUM(Salary), AVG(Salary)
FROM Employees
Group by Job_ID;
Q16: Write a query to display the number of people with the same job.
A16:
SELECT count(*)
FROM Employees
Group by Job_ID;
Q16: Write a query to display the number of people working in same department.
A16:
SELECT count(*)
FROM Employees
Group by Department_ID;
Q17: Write a query to display Department name, number of employees and average salary of all
employees in that department.
A17:
SELECT d.Department_Name,count(*),avg(e.salary)
FROM Employees e INNER JOIN Departments d ON (e.Department_ID=d.Department_ID)
GROUP BY d.Department_Name;
Q18: Write a query to display employee ID, last name and salary of all the employees who earns
more than avg. salary.
A18:
SELECT Employee_ID,Last_Name,Salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
Q19: Write a query to display employee ID, last name and hire date of any employee in the same
department as Jack. Exclude Jack.
A19:
SELECT Employee_ID, Last_Name, Hire_Date
FROM employees
WHERE First_Name <> 'Jack' and department_id = (select department_ID from employees where
First_Name='Jack');
Q20: Select the appropriate answers: (Additional Challenge)
a.) You need to create a procedure named INSERT_LOC that will insert a new location into the
LOCATIONS table. Identify the correct procedure code to perform the preceding task?
i) CREATE PROCEDURE INSERT_LOC (p_lid NUMBER, p_city VARCHAR2)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE ('PROCEDURE STARTED');
INSERT INTO LOCATIONS (location_id, city) VALUES (p_lid, p_city);
END;
b.) You need to create a procedure named INSERT_DEPT that will insert a new department details
into the DEPARTMENTS table. Identify the correct procedure code to perform the preceding task?
i) CREATE PROCEDURE INSERT_DEPT (id NUMBER, name VARCHAR2)
IS
BEGIN
INSERT INTO DEPARTMENTS VALUES (id,name);
END;