Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

MY SQL 2022

Working with SELECT


Sample Database;

Use mydb;

drop table employee;

CREATE TABLE employee


(
id INT,
name VARCHAR(20),
gender VARCHAR(1),
sal INT,
doj DATE,
dno INT
);

insert into employee values(11,'Ganesh','M',65000,'2020-05-07',20);


insert into employee values(22,'Mahesh','M',45000,'2019-03-22',30);
insert into employee values(33,'Suresh','M',95000,'2015-05-17',10);
insert into employee values(44,'Naresh','M',75000,'2017-05-10',20);
insert into employee values(55,'Somesh','M',85000,'2014-08-07',30);
insert into employee values(66,'Jones','M',85000,null,30);
insert into employee values(77,'Alice','F',55000,'2022-05-17',10);
insert into employee values(88,'Suma','F',85000,'2018-05-17',20);

*To display all employee;


select * from employee;

*To get selected columns:


SELECT id, name, dno FROM employee;

***Using where clause with SELECT query


select * from employee where dno = 20;
select * from employee where doj='2020-05-07';

*Display all employee whose salary is less than 60000


select *from employee where sal < 60000;

*Display all employee whose salary is greater than 60000


select *from employee where sal > 60000;

*Display all employees data except dno 10;


select * from employee where dno <> 10;

*Display employee whose name Ganesh or id is 11


select * from employee where name = 'Ganesh' or id=11;
MY SQL 2022
select * from employee where name = 'Ganesh' or id=111;

select * from employee where name = 'Ganes' or id=111;


//no data

*Display employee whose name Ganesh and id is 11


select * from employee where name = 'Ganesh' and id=11;

select * from employee where name = 'Ganesh' and id=101;


//no data

Using Special Operators in SELECT query

IN NOT IN
BETWEEN NOT BETWEEN
LIKE NOT LIKE
IS NULL IS NOT NULL

IN / NOT IN:
➢ It picks one by one value from the given list.
➢ It supports with all types of data.

Examples;
*Display employees whose id’s are 11,33 and 55;
select * from employee where id in(11,33,55);

*Display employees except whose id’s are 11,33 and 55;


select * from employee where id not in(11,33,55);

BETWEEN / NOT BETWEEN


➢ It fixes the values within a range
➢ It supports with numeric and date values only.

Example:
select * from employee where id between 11 and 44;
select * from employee where id not between 11 and 44;

*Display employees whose doj between '2015-01-01' and '2020-01-01'


select * from employee where doj between '2015-01-01' and '2020-01-01';

*Display employees whose doj not between '2015-01-01' and '2020-01-01'


select * from employee where doj not between '2015-01-01' and '2020-01-01';
MY SQL 2022

LIKE / NOT LIKE


➢ It is used to search specific pattern in the given input;
➢ It supports with character information only.
➢ It uses 2 Meta Characters:
‘_’ (underscore) represents single character.
‘%’ (percentage) represents ‘zero’ or more characters.

Examples:
*List the employees whose name starts with ‘S’
select * from employee where name like 'S%';

*List the employs whose name starts with ‘S’ or ‘s’


select * from employee where name like 'S%' or name like 's%';

* List the employs whose name having 5 characters.


select *from employee where name like '_____'; Select all records
where the value of the
name column contains
* List the employees whose name having second letter is a.
the letter "a".
select * from employee where name like '_a%';
select *from employee
List the employee whose name ends with 'h'
where name
select *from employee where name like '%h';
like '%a%';
IS NULL / IS NOT NULL
➢ It is used to compare the NULL values.

Examples:

*List the employees whose doj is nothing.


select *from employee where doj is null;

*List the employees whose doj is not null


select *from employee where doj is not null;

*Assign current system date to the employees whose doj is nothing


UPDATE employee SET doj=NOW() WHERE doj is null;

Select all records where the value of the name column starts with
letter "a" and ends with the letter "b".

select *from employee where name like 'a%b';


MY SQL 2022
Using Aggregate Functions or Group Functions with SELECT query.

➢ These are applied on group of rows


➢ It considered entire column content as one input

Name Usage
count Gives no. of rows in the given column
sum Gives total on the given column
avg Gives average of the given column
min Picks the smallest value from the given column.
max Picks the largest value from the given column

Example:
SELECT count(*), sum(sal), avg(sal), min(sal), max(sal) FROM employee;

*Using alias names


select count(*) COUNT, sum(sal) SUM, avg(sal) AVG, min(sal) MIN, max(sal) MAX from employee;
MY SQL 2022
GROUP BY clause with SELECT query:
➢ It is used to group the rows based on the specified column.
➢ Whenever an ordinary column retrieved along with aggregate values then all ordinary columns
must be specified after GROUP BY clause.

Examples;

*Find the sum of salaries in each dept.


SELECT dno, sum(sal) FROM employee GROUP BY dno;

*Find the average salary in each dept.


SELECT dno, avg(sal) FROM employee GROUP BY dno;

*Find the least salary in each dept.


SELECT dno, min(sal) FROM employee GROUP BY dno;

*Find the max salary in each dept.


SELECT dno, max(sal) FROM employee GROUP BY dno;

select dno, sum(sal),avg(sal),min(sal),max(sal),count(*) from employee group by dno;

Using HAVING clause with SELECT query:


➢ It is used to apply the condition on grouped results.

Example:

*Display sum of salaries of dept whose sum of salaries is more than 100000
SELECT dno, SUM(sal) FROM employee GROUP BY dno HAVING sum(sal) > 100000;

*Display average of each dept where avg is greater than 80000


SELECT dno, avg(sal) FROM employee GROUP BY dno HAVING avg(sal) > 80000;
MY SQL 2022
ORDER BY clause with SELECT query:
➢ It is used to arrange the selected output in an order whether ascending order (or) descending.
➢ It supports with all types of data.
ASC-----------ascending order
DESC---------descending order

Examples:
SELECT *FROM employee ORDER BY sal;
SELECT *FROM employee ORDER BY name;

SELECT *FROM employee ORDER BY sal DESC;


SELECT *FROM employee ORDER BY name DESC;
MY SQL 2022
Sub Queries:
➢ A query within a query is called Sub Query

➢ SELECT statement is provided in the conditional clause(WHERE, HAVING)

➢ In a sub query, first inner query is executed and based on the output of inner query, outer query
will be executed.

➢ Without sending multiple queries to the DB server to get the result, by using Sub Queries we can
send a single query to DB s/w to get response.

➢ Sub queries improve performance of the application while retreiving or manipulating data.

Examples;

*List the employs who belongs to 'Ganesh' dept. (without using sub query)
SELECT dno FROM employee WHERE name='Ganesh';
SELECT * FROM employee WHERE dno=20;

*List the employs who belongs to Rupesh dept. (using sub query)
SELECT * FROM employee WHERE dno=(SELECT dno FROM employee WHERE name='Ganesh');

*List the employ who received max salary.


SELECT * FROM employee WHERE sal = (SELECT MAX(sal) FROM employee);

To find the Nth highest salary.

SELECT DISTINCT(sal_column) FROM table ORDER BY sal_column DESC LIMIT n-1,m;

Limit clause has two components, First component (n-1) is to skip number of rows from top and

second component (m) is display number of rows we want.

*Display 1st highest salary


SELECT DISTINCT(sal) FROM employee ORDER BY sal DESC LIMIT 0,1;

*Display 2nd highest salary


SELECT DISTINCT(sal) FROM employee ORDER BY sal DESC LIMIT 1,1;

*Display 3rd highest salary


SELECT DISTINCT(sal) FROM employee ORDER BY sal DESC LIMIT 2,1;

You might also like