Rai SQLassignment[1]
Rai SQLassignment[1]
Emp_name Varchar2 20
Dept_id Number 20
Query:-
Query:-
Dept_name Varchar2 10
Query:-
Query:-
Query:-
ii)
Use a LEFT OUTER JOIN to list all employees, showing their
department if they have one. If an employee does not belong to a
department, show NULL for the department name.
Query:-
Select Emp_name, Dept_name
From Employees left outer join Departments on Employees.Dept_id
= Departments.Dept_id;
iii)
Use a RIGHT OUTER JOIN to list all departments, showing the
names of employees in each department. If a department has no
employees, show NULL for the employee’s name.
Query:-
Use a FULL OUTER JOIN to list all employees and all departments,
showing NULL for departments with no employees and employees
with no department.
iv)
Query:-
SELECT Employees.Emp_name, Departments.Dept_name
FROM Employees
LEFT JOIN Departments ON Employees.Dept_id = Departments.Dept_id
UNION
SELECT Employees.Emp_name, Departments.Dept_name
FROM Departments
RIGHT JOIN Employees ON Employees.Dept_id = Departments.Dept_id;
Use an INNER JOIN to find the names of employees who are in both
IT and HR departments.
v)
Query:-
vi)
Query:-
vii)
Query:-
SELECT Departments.Dept_name,
COUNT(Employees.Emp_id) AS Total_Employees
FROM Employees INNER JOIN Departments ON
Employees.Dept_id = Departments.Dept_id
GROUP BY Departments.Dept_name;
Find all employees who do not belong to either the "HR" or "IT"
department using an OUTER JOIN.
Query:-
viii)
WHERE Departments.Dept_name NOT IN ('HR', 'IT')
OR Departments.Dept_name IS NULL;
Query:-
SELECT e.Emp_name, d.Dept_name
FROM Employees e JOIN
Departments d ON
e.Dept_id = d.Dept_id
WHERE Dept_name NOT LIKE 'M%';
ix)
Use a LEFT JOIN to list all employees and count how many
employees are in each department, even if some departments have
no employees.
Query:-
GROUP BY d.Dept_name;
x)
Find the department name and employee name for employees who
have no department assigned by using an OUTER JOIN.
Query:-
SELECT e.Emp_name, d.Dept_name
FROM Employees e LEFT JOIN Departments d
ON e.Dept_id = d.Dept_id
WHERE d.Dept_id IS NULL;
xi)
Query:-