12. SQL Commands Part 4 - Uchiha3000/LINUX_docx GitHub Wiki

SQL Commands Part 4

Display the total salary after calculating the commission pct?

code: select employee_id, first_name, salary, commission_pct, salary+(salary*commission_pct) "Total Salary" from employees;

[Note: The problem with this query is that if the commission_pct value is null then the total salary will also be null. To avoid this we use this query below. Null is a junk value, so we cannot use arithmetic operator along with it. ]

code: select employee_id, first_name, salary, nvl(commission_pct,0), salary+(salary*nvl(commission_pct,0)) "Net Salary" from employees;

[Note: In this nvl function we convert null value of commission_pct to 0. nvl stands for null to value.]

Difference between round and trunc function.

code: select round (243.567,2), trunc (243.56712) from dual;

image image

Display the number of employees working?

code: select count(*) from employees;

Display the number of employees working in department 50?

code: select count(*) from employees where department_id=50;

Display the total number of employees working in each department?

code: select department_id, count(*) from employees group by department_id;

image

[Note: As previously told we can't use aggregate function or single value return function with field name or column name unless we use another clause 'group by' along with the field name that we used with the aggregate function.]

Display the department id and number of employees from all department except department 50.

code: select department_id, count(*) from employees where department_id <> 50 or department_id is null group by department_id;

Or

code: select department_id, count(*) from employees where department_id != 50 or department_id is null group by department_id;

image

[Note: We can use not equals to like this <> or !=]

Display maximum salary of each department.

code: select department_id, max(salary) from employees group by department_id;

image

Display those department where less than 5 employees are working?

code: select department_id, count( * ) from employees group by department_id having count( * )<=5;

[Note: We are using having clause instead of where clause because. With where clause we cannot use single value return function, although we can use multiple value return function like upper, lower, length since they can return multiple value. But count, max, min, avg these functions we cannot use with where clause, so we are using having clause. It's important to note that if we are using having clause we need to use group by clause, because without it we cannot use having clause.]

Display those first_name that are duplicate.

code: select first_name, count( * ) from employees group by first_name having count( * )>1;

image image

Structure code: select < field name > from <table/view name> where < condition > group by < field name > having < condition > order by < field >.

Display maximum salary?

code: select max(salary) from employees;

image

Display who is earning the maximum salary of 24000.

code: select employee_id, first_name, salary, department_id from employees where salary=24000;

image

Display the employee details of those who are earning maximum salary?

code: select employee_id, first_name, salary, department_id from employees where salary=( select max(salary) from employees);

image

[Note: The first part that will be executed will be the part that is within (). The outer part will take the value of inner part and execute.]

Display those employees who are earning more than the average salary?

code: select employee_id, first_name, salary, department_id from employees where salary> (select avg(salary) from employees);

image

Display those employees who are earning second highest salary?

code: select employee_id, first_name, salary, department_id from employees where salary=(select max(salary) from employees where salary <(select max(salary) from employees));

image

[Note: The innermost query will find the highest salary then the second innermost query will select all salary that is just lower to the highest salary. Finally the outermost query will display all the employee details that has the second most highest salary. ]

[innermost query- "(select max(salary) from employees)"

second innermost query- "(select max(salary) from employees where salary <(select max(salary) from employees))"

outermost query- "select employee_id, first_name, salary, department_id from employees where salary=(select max(salary) from employees where salary <(select max(salary) from employees)); "]

Display how many employees are hired per year from the beginning?

code: select extract(year from hire_date), count(*) from employees group by extract(year from hire_date) order by extract(year from hire_date);

image

Display how many employees are hired based on month?

code: select extract(month from hire_date), count(*) from employees group by extract (month from hire_date) order by extract(month from hire_date);

image

Show me those employees who are earning maximum salary in their department.

code: select emplopyee_id, first_name, salary, department_id from employees where (department_id, salary) in (select department_id, max(salary) from employees group by department_id) order by department_id;

image
⚠️ **GitHub.com Fallback** ⚠️