-
Notifications
You must be signed in to change notification settings - Fork 0
11. SQL Commands Part 3
Ans: Null is neither 0 nor space. It is basically a junk value.
code: select employee_id, first_name, salary, commission_pct from employees where commission_pct is not null;
code: select employee_id, first_name, salary, commission_pct from employees where commission_pct is null;
Display those employees whose department_id is null or employees who desn't belong to any department?
code: select employee_id, first_name, salary, commission_pct from employees where department_id is null;
code: select count(employee_id) from employees;
code: select count(commission_pct) from employees;
Ans: count(*) counts all rows that it finds any value in. You can understand it by watching the image below.
Ans: select sum(salary) from employees;
Ans: select avg(salary) from employees;
code: select round(avg(salary),2) from employees;
code: select sum(salary)/count(*) from employees;
code: select max(salary), min(salary) from employees;
[Note: With single value return functions we cannot use any field name. Single value return functions are those functions that return single value like avg, min, max, etc... This happens because SVR functions gives 1 value and field name or columns have multiple values. ]
code: select count(*) "Total no of Employees", sum(salary), max (salary) from employees;
[Note: This works because every single one of them is a single value return function.]
code: select lower(first_name), upper(last_name) from employees;
code: select employee_id, first_name, salary, department_id, job_id from employees where lower(first_name) like 'peter';
code: select job_id, initcap(job_id) from employees;
[Note: initcap concerts the first letter to uppercase and the rest to lowercase. initcap full form is initial capital.]
code: select first_name, length(first_name) from employees;
[Note: Remember it will count space too. Like for 'Josh Manual' it will return 11.]
There are two kinds of padding left padding and right padding.
code: select first_name, lpad(first_name, 20, #) from employees; [The 20 is for 20 characters. It means that the first name will be displayed as 20 characters long and to fill the blank spaces it uses #. It wouldlook something like this '###############Akash']
code: select first_name, rpad(first_name, 20, #) from employees;
code: select first_name, substr(first_name, 1, 4) from employees;
[substr takes three fields 1st is name, 2nd is position in this case from 1st character , 3rd is no of characters to be taken in this case 4]
code: select first_name, substr(first_name, length(first_name)-3, 4) from employees;
code: select first_name, substr(first_name, -4, 4) from employees;
code: select first_name, replace(first_name, 'a', '@') from employees;
[Note: Problem with this query is that it will only replace small letter a's not capital letter A's. To solve that.]
code: select first_name, replace(upper(first_name), 'A', '@') from employees;
[Note: Now it will replace all capital A's with @.]
code: select first_name, instr(first_name,e) from employees;
[Note: The problem with this query is that it will not take in account of the first characters since they are in capital letter, so to remove this issue we need to convert the first_name inside instr to lower case.]
code: select first_name, instr(lower(first_name),e) from employees;
code: select first_name from employees where length(first_name)=5;
code: select salary, power(salary, 2) from employees;
code: select hire_date, to_char(hire_date, 'dd-mm-yyyy') from employees;
code: select hire_date, to_char(hire_date, 'day, dd-Mon-yyyy') from employees;
code: select sysdate from dual;
[Note: There is only 1 dummy field in the temporary table dual called sysdate.]
code: select hire_date, to_char(hire_date, 'yyyy') from employees;
code: select hire_date, extract(year from hire_date) from employees;
code: select hire_date, months_between(sysdate, hire_date) from employees;
[Note: anything we write from keyboard gets converted to string, to undo that we use to_date.]
code: select months_between (sysdate, to_date('19-dec-2024','dd-mm-yyyy')) from dual.
[Note: Whenever we are typing any date from keyboard it gets converted to string to change this conversion to date we use to-date.]
code: select add_months(sysdate, 2) from dual;
code: select last_date(sysdate) from dual.
code: select next_day(sysdate,'Monday') from dual;
code: select next_day(sysdate,'Friday') from dual;
code: select employee_ide, first_name, salary, hire_date from employees where hire_date > to_date('31-12-2006 ','dd-mm-yyyy');
code: select employee_ide, first_name, salary, hire_date from employees where hire_date > to_date('31-12-2006 ','dd-mm-yyyy') and hire_date < to_date('01-01-2008','dd-mm-yyyy');
code:select employee_ide, first_name, salary, hire_date from employees where hire_date between to_date('31-12-2006 ','dd-mm-yyyy') and to_date('01-01-2008','dd-mm-yyyy');
code: select hire_date, extract(year from sysdate) -extract(year from hirer-date) from employees;
code: select , from <table / view name> where order by