0% found this document useful (0 votes)
10 views

Day 5 - SQL Commands - YouTube

Uploaded by

santhosh G
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Day 5 - SQL Commands - YouTube

Uploaded by

santhosh G
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Day 5 - Distinct, Order By, Limit, Aggregate functions, Group By, Having

create table employee(


id SERIAL primary key,
f_nm varchar(20) not null,
l_nm varchar(20) not null,
age int not null,
location varchar(20) not null default 'Hyderabad',
dept varchar(20) not null
);

alter table employee add salary real not null;

select * from employee;

insert into employee (f_nm,l_nm,age,dept,salary) values


('Ravi','Kiran',25,'HR',30000.00);
insert into employee (f_nm,l_nm,age,dept,salary) values
('Priya','Darshini',28,'HR',32000.00),
('Mohan','Bhargav',35,'IT',40000.00),
('Manoj','Bajpai',40,'IT',45000.00);

insert into employee (f_nm,l_nm,age,location,dept,salary) values


('Akhil','K',26,'Bangalore','IT',42000.00),
('Raja','Roy',35,'Bangalore','IT',60000.00),
('Shilpa','Sharma',40,'Chennai','IT',44000.00);

select * from employee;

DISTINCT - unique values, no repetition


Select location from employee;
select distinct location from employee;
select distinct dept from employee;

select count(distinct dept) from employee;

ORDER BY - Sort the Data, arrange the data in a sequence, either ascending order
(default) or in descending order (desc)
select f_nm from employee;
select f_nm from employee order by f_nm;
select f_nm from employee order by f_nm desc;
select f_nm from employee order by age;
select f_nm from employee order by age desc;
select * from employee order by age, salary; ---second level sort will happen
incase of a clash.

LIMIT - to put a limit on the number of records to be fetched (filter - eliminate


what is not required)
select * from employee limit 3;
select * from employee order by salary limit 3; - Bottom 3 employees by Salary
select * from employee order by salary desc limit 3; - Top 3 employees by Salary
select * from employee order by age limit 5;
select * from employee order by age,salary limit 4;

select id,f_nm,l_nm from employee order by id limit 1 offset 0; --- gives the
first record
select id,f_nm,l_nm from employee order by id limit 3 offset 3; --- beginning in
the 4th place it will give 3 records.
___________________________________________________________________________________
_________________________________

AGGREGATE FUNCTIONS - Sum, Avg, Min, Max, Count, Count Distinct ....
To get the total number of records - select count(*) from employee;
From how many locations are people joining - select count(location) from employee;

From how many distinct locations are people joining - select count(distinct
location) from employee;
To give an alias name to the column - select count(distinct location) as
num_of_locations from employee;
To get the number of people above 30 years - select count(f_nm) from employee where
age>30;
select count(f_nm) from employee where age>25 and age <35;

Total salaries being paid to employees - select sum(salary) from employee;


Average of the salaries being paid to the employees - select avg(salary) from
employee;
similarly minimum and maximum also can be done.

Identify the youngest employee - select min(age) from employee; gives the minimum
age, but to know who is the employee - select f_nm, l_nm from employee order by age
limit 1;

You might also like