You are on page 1of 4

-- clauses :

-- where : to filter rows


-- order by : sort the data
-- select * from table_name order by column_name ;

-- distinct : fetch unique values of a column


-- select distinct column_name from table_name;
-- select distinct * from table_name;

-- limit offset,no of rows to read :


-- 15 t0 20 , limit 14,6;

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


-- select min(salary) from table_name;

-- group by : groups the data


-- having : to filter groups

-- select from where group by having order by limit;

select * from dept;

-- fetch the count of emp who requires to travel or not for each dept
select dept_name,travel_required,count(*)
from dept
group by dept_name,travel_required
order by dept_name;

-- fetch the count of emp who requires to travel for each dept
select dept_name,count(*)
from dept
where travel_required='yes'
group by dept_name
order by dept_name;

-- Subquery :
-- select : outer query (select :inner query : subqueries
-- create
-- insert

-- scalar subqeries : inner query returns a single value

select * from emp_data;


select max(Salary) from emp_data; -- 197537

-- fetch the details of an emp who holds this salary


select * from emp_data where salary=197537;
select * from emp_data where salary=(select max(salary) from emp_data);

-- second highest salary using max function


/*
90
80 -- second highest salary
70
60
50
*/

-- details of amp having second highest salary

select * from emp_data where salary=


(select max(salary) from emp_data where salary <(select max(salary) from
emp_data));

-- details of emp having 9th highest salary


select * from emp_data where salary=(
select distinct salary from emp_data order by salary desc limit 8,1);

-- fetch the name and salary of emp whose salary > avg salary
select * from emp_data;
select fname,salary from emp_data where salary >
(select avg(salary) from emp_data);

-- fetch the emp names of dept id 30 having salary > avg salary of dept id
20
-- avg salary of emp from dept_id 20
-- compare this salary with salaries of emp from dept id 30

select fname from emp_data where dept_id=30 and salary>


(select avg(salary) from emp_data where dept_id=20);

select * from movies;


-- fetch a movie name which has min budget and max revenue
-- min (budget)
-- max(revenue)
-- movie name which satisfy both of the above filters

select `movie name` from movies where


`budget(inr)`=(select min(`budget(inr)`) from movies)
and `revenue(inr)`=(select max(`revenue(inr)`) from movies);

select `movie name` from movies where


`budget(inr)`=(select min(`budget(inr)`) from movies)
or `revenue(inr)`=(select max(`revenue(inr)`) from movies);

select * from movies where `budget(inr)`=(select min(`budget(inr)`) from


movies);
select * from movies where `revenue(inr)`=(select max(`revenue(inr)`) from
movies);

-- multirow subqueries: inner query returns more than one value

-- fetch movie name,genre,star rating where star_rating= star_rating of


any genre
-- max start rating for each genre
-- compare star ratings of all movies with the above result
select * from movies;
select `movie name`,genre,star_rating FROM MOVIES where star_rating IN
(select max(star_rating) from movies group by genre);

select * from emp_data;


select * from dept;

-- fetch the emp id from emp_data which are not present in dept

select empid from emp_data where empid NOT IN


(select distinct empid from dept);

-- union : appends the result sets


/*
a b : a b :
a
b
*/

select * from tbl1


UNION
select * from tbl2;

-- 10 + 5 (3) == 10 +5-3 = 12 values

select * from tbl1


UNION ALL
select * from tbl2;

select empid from emp_data


union
select empid from dept
union
select dept_id from dept;

select count(*) from emp_data;

select count(*)
from (
select * from tbl1
UNION ALL
select * from tbl2) SQ;

select col1 col from tbl1;

select salary,salary*0.25 as per_sal from emp_data e;

-- import pandas as pd

-- JOINS : to combine data from two or multiple tables


/*
inner join : common records present in both tables
left join :
right join :
full join :
*/

-- where empid=1001
-- tbl1.col1=tbl2.col2

-- INNER JOIN
select * from tbl1;
select * from tbl2;

select * from tbl1 INNER JOIN tbl2 ON tbl1.col1=tbl2.col2;


select tbl2.* from tbl1 INNER JOIN tbl2 ON tbl1.col1=tbl2.col2;

-- employee and dept


select emp_id,emp_name,dept_name
from employee
inner join department
ON employee.dept_id=department.dept_id;

select emp_id,emp_name,employee.dept_id,dept_name
from employee
inner join department
ON employee.dept_id=department.dept_id;

select empid from emp_data;

select e.emp_id,emp_name,e.dept_id id,dept_name name


from employee e
inner join department d
ON e.dept_id=d.dept_id;

select s.roll_no,stud_name,course_name from


student s
inner join
course c
on s.roll_no=c.roll_no;

create table student as select * from engg_data.student;


create table course as select * from course;

-- LEFT JOIN

You might also like