SQL Day7

You might also like

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

use stud_data;

select emp_id,emp_name from emp;


-- select column_name,count(*) from table group by column_name;
select * from cars;

-- total distance travelled by each car


select cars,sum(distance_travlled) as dis_trav from cars group by cars;

select * from paintings;

-- no of paintings an artist have sold


select artist_id,count(artist_id) from paintings group by artist_id;

select * from project;


select distinct proj_name from project;
select proj_name from project group by proj_name;

select * from placement;


select distinct branch from placement; -- unique fun
select count(branch) from placement;

select count(distinct branch) from placement; -- nunique fun

-- count of rows in this output


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

-- select count(*) from table_name;

select count(*) from (


select branch from placement group by branch ) sq;

-- subqueries : query within query


-- scalar subquery : inner query returns only single value
-- multirow subquery: inner query returns more than one value

-- fetch emp details of an emp having max salary


-- max salary
-- details of an emp who has this max salary

select max(salary) from emp; -- 10000


select * from emp where salary=10000;
select * from emp where salary=(select max(salary) from emp);

-- fetch the emp names whos salary> avg salary


select * from emp;
-- avg salary
-- fetch emp names of emp having salary> avg salary
select emp_name,salary from emp where salary>
(select avg(salary) from emp);

select avg(salary) from emp;

-- oldest emp from company(consider min empid is the oldest emp)


select * from emp;
-- min empid
-- details of emp with min empid
select * from emp where emp_id=(select min(emp_id) from emp);

-- oldest emp from IT department


-- min(empid) from IT dept
-- details of this emp

select * from emp where dept_name='IT' and emp_id=(select min(emp_id) from


emp);
select min(emp_id) from emp where dept_name='IT';

select * from emp where emp_id=(select min(emp_id) from emp where


dept_name='IT');

-- recent emp of HR who has salary>5000


-- max empid of dept= HR and salary >5000
-- details of this emp

select * from emp where emp_id=(


select max(emp_id) from emp where dept_name='HR' and salary>5000);

select * from artists;


select * from sales;

-- Name of artists who have sold atleast one painting


-- select unique artist id we have in sales table
-- fetch artist names whos ID is in above output

select * from artists where id IN (select distinct artist_id from sales);


select * from artists where fname IN (select distinct artist_id from
sales); -- logically wrong

-- Name of artists who have not sold anything


select * from artists where id NOT IN (select distinct artist_id from
sales);

select * from student;


select * from course;

-- fetch students whos info is missing from course table


-- fetch distinct roll_no we have in course table
-- fetch students from student who are missing in above output

select roll_no from course;


select * from student where roll_no NOT IN (select roll_no from course);

select * from tbl1;


select * from tbl2;

-- fetch values from tbl1 which are not tehre in tbl2


-- select distinct values from table2
-- select values from tbl1 which are not tehre in above output
select col1 from tbl1 where col1 NOT IN(
select distinct col2 from tbl2);

-- joins : to combine the data from multiple db tables


/*
inner join/join : common record present in both tables
left join/left outer join : inner join + remaining records from left
table
right join/right outer join: inner join + remaining records from right
table
full join/full outer join : right join + left join -- append the output
of left and right join

A left join B
left join C inner join D:

A A : join : single row and 2 columns


a
a : two rows and one columns
*/

select * from tbl2;


-- ON : to specify join condition
-- where column=value
-- tbl1.coulmn_value=tbl2.column_value

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

select * from student;


select * from course;

-- roll_no,stud_name and their course name

select * from student;


select roll_no,stud_name from student;

select *
from student inner join course ON student.roll_no=course.roll_no;

select student.roll_no,course.roll_no,stud_name,course_name
from student inner join course ON student.roll_no=course.roll_no;

select s.roll_no,c.roll_no,stud_name,course_name
from student s
inner join course c
ON s.roll_no=c.roll_no;

-- LEFT JOIN :
select * from tbl1 LEFT JOIN tbl2 ON tbl1.col1=tbl2.col2;
select TBL1.* from tbl1 LEFT JOIN tbl2 ON tbl1.col1=tbl2.col2;
select tbl2.*,tbl1.* from tbl1 LEFT JOIN tbl2 ON tbl1.col1=tbl2.col2;

select student.roll_no,course.roll_no,stud_name,course_name,course_id
from student left join course ON student.roll_no=course.roll_no;

You might also like