You are on page 1of 5

use stud_info;

/*
unique : dups not allowed,null allowed
primary key : no dups and no nulls
not null : no null values
foreign key : to maintain referential integrity
default : to set a default value for some column
check : to check for a condition before inserting data
auto_increment : to increment a column value by 1 for each insertion
*/

-- CLAUSES:
-- ORDER BY : sort the data
-- default sort order is ascending
-- for descending sort, use DESC keyword in order by clause

select * from emp_data;


select * from emp_data where dept_id=20 order by loc;
select * from emp_data where dept_id=20 order by loc DESC;

-- DESC table_name : DESCRIBE TABLE


-- ORDER BY DESC : DESCENDING SORT
-- select * from table_name where condition order by
column_name1,column_name2

select * from emp_data where dept_id=20 order by loc desc,salary;

-- DISTINCT : to fetch the unique values from a column


select distinct loc from emp_data;
select distinct dept_id from emp_data;
select distinct gender from emp_data;
select distinct empid from emp_data;

/*
1001 rahul 40000 pune
1001 rahul 40000 pune
*/

select distinct * from emp_data; -- unique records

-- LIMIT : limits the no of rows in the output


-- offset : no of rows to skip from top,default value of offset is 0
-- no of rows to read
select * from emp_data limit 0,5;
select * from emp_data limit 5;

-- top 15 rows
select * from emp_data limit 15;

-- top 15 salary values from dept_id 10,20


select * from emp_data where dept_id in (10,20)
order by salary desc limit 15;
-- fetch rows from 10 to 15
select * from emp_data limit 9,6;

-- skip 5 rows and read 10 rows after that


select * from emp_data limit 5,10;

-- fetch rows from 7 to 10


select * from emp_data limit 6,4;

-- 8th row
select * from emp_data limit 7,1;

-- 5th highest salary from emp


select * from emp order by salary desc;
select distinct salary from emp order by salary desc limit 4,1;

-- dept_name=IT
select distinct salary from emp where dept_name='IT'
order by salary desc limit 4,1;

-- 9th highest salary


select distinct salary from emp order by salary desc limit 8,1;

-- nth highest salary


-- select distinct salary from emp order by salary desc limit n-1,1;

-- select distinct from where order by limit

-- aggregate functions:
-- sum,min,max,avg,count

select sum(salary) from emp_Data;


select min(salary) from emp_Data;
select max(salary) from emp_Data;
select avg(salary) from emp_Data;

select * from student;


select count(*) from student; -- total no of rows in the output
select count(1) from student; -- total no of rows
select count(*) from student where address='mumbai';

select count(roll_no) from student; -- count of non null values


select count(stud_name) from student;

select count(*),sum(salary),avg(salary),min(salary),max(salary) from


emp_data;

select * from emp;

-- fetch min salary from each dept


select distinct dept_name from emp;
select min(salary) from emp where dept_name='admin';
select min(salary) from emp where dept_name='hr';
select min(salary) from emp where dept_name='IT';
select min(salary) from emp where dept_name='FINANCE';

-- GROUP BY : create groups of data based on simillar values


select dept_name,count(*) from emp group by dept_name order by dept_name;

select * from emp;


select dept_name,count(emp_id),avg(salary) from emp group by dept_name
order by dept_name;

select * from movies;


-- fetch avg budget of movies released during normal days
select avg(`budget(inr)`) from movies where `release period`='normal';

-- total no of screens on which drama movies were released


select sum(`number of screens`),count(`number of screens`) from movies
where genre='drama';

select * from placement;


-- avg cgpa of mech students
select avg(cgpa) from placement where branch='mechanical';

-- count of all students


select count(*) from placement; -- 2666

-- list of students cgpa>5,historyofbacklogs=0 and internship>0


select * from placement
where cgpa>5
and historyofbacklogs=0
and internships>0;

-- count of these students


select count(*) from placement
where cgpa>5
and historyofbacklogs=0
and internships>0;

-- branch wise count


select branch,count(*),avg(cgpa) from placement
where cgpa>5
and historyofbacklogs=0
and internships>0
group by branch;

-- only branches where count(students)>150


select branch,count(*),avg(cgpa) from placement
where cgpa>5
and historyofbacklogs=0
and internships>0
group by branch
having count(*)>150;
-- having cgpa>5;

-- min salary from emp table


select * from emp;
select min(salary) from emp;

-- min salary from each dept


select dept_name,min(salary) from emp group by dept_name;

select dept_name,max(salary) from emp group by dept_name;

select * from emp_Data;

-- fetch count of emp and their avg salary from each loc
select loc,count(*),avg(salary) from emp_data group by loc;

-- count of male and female emp


select loc,gender,count(*),avg(salary) from emp_data group by loc,gender
order by loc;

select gender,count(*) from emp_Data where loc='chicago' group by gender;

select * from placement;

-- count of stud placed and not from each branch


select branch,placedornot,count(*) from placement group by
branch,placedornot
order by branch;

-- count of students placed from each branch


select branch,placedornot,count(*) from placement
where placedornot=1
group by branch,placedornot
-- having placedornot=1
order by branch;

-- only groups where count(*) <200


select branch,placedornot,count(*)
from placement
where placedornot=1
group by branch,placedornot
having count(*)<200
order by branch
limit 1,1;

select placedornot,count(*) from placement


where branch='electrical'
group by placedornot;

-- select from where group by having order by limit

select * from movies;

-- count of movies which are released during holidays and their avg
revenue for each genre
-- select from order by limit group by
select genre,count(*),avg(`revenue(inr)`)
from movies
where `release period`='holiday'
group by genre
having count(*)>50;

You might also like