You are on page 1of 3

/*

DDL: create,alter,truncate,drop : create, change or delete the structure


DML: insert,update,delete : create, change or delete the data
DCL: grant, revoke
TCL: commit,rollback,savepoint
DQL: Select : read data
*/
-- student

select * from student; -- all columns , all rows


select roll_no from student; -- 1 column and all rows
select * from student where roll_no=104; -- all columns ,specific row
select stud_name from student where roll_no=104; -- specific oclumn of
specific rows

-- update
update student set address='chennai'; -- update address value for all rows
-- update address value for rows where stud_name starts with N
update student set address='hbad' where stud_name like 'n%';

-- update roll no and name for students from pune


update student set roll_no=100, stud_name='niharika' where address='pune';

-- and & or : combine multiple where conditions


-- and: both the conditions should be satisfied by a row

-- update address where roll_no=100 and name='niharika'


update student set address='chennai' where roll_no=100 and
stud_name='niharika';
update student set address='chennai' where roll_no=100 or
stud_name='nikita';

-- delete
delete from student;
delete from student where roll_no=100; -- deletes the specific row
truncate table student; -- delets the entire data

select * from student;


desc student;
drop table student; -- deletes data as well as structure

-- order by : sort the data


select * from emp order by salary;
select * from emp order by salary desc;

-- sort the data first on the basis of dept and then its salaries
select * from emp order by salary,dept_name;
select * from emp order by dept_name,salary;

-- limit : to read a range of rows


-- limit offset, no of rows to read, default value of offset is 0

select * from emp limit 5;


-- 9th row
select * from emp limit 8,1;

-- 9th highest salary


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

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

-- emp details who has this 9th highest salary


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

select * from emp where salary=(


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

-- crud : create data,read,update,delete

-- group by :
-- having

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


-- count of all emp
select count(*) from emp;
select dept_name,count(emp_name),avg(salary) from emp group by dept_name;

select * from student_mark;

-- precentage of all students


-- sum(marks)/sum(out_of)*100
select sum(marks)/sum(out_of)*100 from student_mark;
select rollno,sum(marks)/sum(out_of)*100 from student_mark group by
rollno;

select * from sales;


-- fetch the total sale and no of paintings each artist have sold
select artist_id,sum(sales_price),count(id) from sales group by artist_id;

select * from movies;

-- avg budget ,avg revenue of akshay kumar movies


-- released during holidays as well as normal days

/*
emp : table
empid,empname,salary,loc -- columns
1001 rahul 70000 pune -- rows/records
*/

select `release period`,avg(budget),avg(revenue) from movies


where `lead star`='akshay kumar'
group by `release period`;

select * from movies;

-- fetch actor where action movie which is a remake,actor based of avg


revenue

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

select `lead star`,avg(revenue)


from movies
where genre='action'
and `whether remake`='yes'
group by `lead star`;

-- avg revenue : akshay kumar genre=action and it is a remake movie


select avg(revenue)
from movies
where genre='action'
and `whether remake`='yes'
and `lead star`='akshay kumar';

select emp_name,max(salary) from emp;

-- having :
select `lead star`,avg(revenue)
from movies
where genre='action'
and `whether remake`='yes'
group by `lead star`
having avg(revenue)>500000000;

You might also like