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

sqlqueries

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

sqlqueries

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

create database testing;

/*

It is used for selecting the database, which we want to work currently.

*/

use testing;

create table employee

id int,

name varchar(30),

dept varchar(30),

salary int

);

insert into employee values(2,'john','developer',55000);

select * from employee;

select id from employee;

select id,name from employee;

update employee set salary=60000 where id=2;

select id,name,dept,salary from employee;

insert into employee values(3,'rahul','hr');

insert into employee(id,name,dept) values(3,'rahul','hr');

insert into employee(id,name,salary) values(4,'manish',56000);


insert into employee values(11,'rr','marketing',52000,23),

(12,'ms','marketing',63000,37),

(13,'bharat','developer',62000,19);

select * from employee;

select id,name, salary as sal , (salary*2) as bonus from employee;

alter table employee add (age int);

update employee set age=29 where id>4;

select * from employee where dept='developer';

/*

check for both the condtion should be developer and age should be lessthan 30

*/

select * from employee where dept='developer' and age<30;

/*

any one is true , it will reterive the record.

*/

select * from employee where dept='developer' or age<25;

select * from employee where dept = 'hr';

select * from employee where dept <> 'hr';

select * from employee where dept <> 'hr' and salary<60000;

select id,name,dept,salary from employee where dept <> 'hr' and salary<60000;

select * from employee order by name;


select * from employee order by salary ;

select * from employee order by age asc;

select * from employee order by name desc;

select * from employee order by age desc;

select * from employee where dept='developer' order by salary desc;

select name, upper(name) from employee;

select name, lower(name) from employee;

select name, length(name) from employee;

select max(salary) from employee;

select min(salary) from employee;

select count(salary) from employee;

select sum(salary) from employee;

select avg(salary) from employee;

select * from employee;

select dept from employee group by dept;

select name from employee group by name;

select age from employee group by age;

select dept,count(id),max(salary),min(salary) from employee group by dept;

/*

dept wise sum salary


*/

select dept, sum(salary) from employee group by dept;

select dept, max(salary) from employee group by dept;

select dept,count(id) from employee group by dept;

/* dept wise employee count, except tester dept.

*/

select dept,count(id) from employee

where dept <> 'tester'

group by dept;

/* dept wise employee count, except tester dept.display the count in descending order

*/

select dept,count(id) from employee

where dept <> 'tester'

group by dept

order by count(id) desc;

select dept,max(salary) from employee

group by dept;

select * from employee;

select dept,count(id) from employee

group by dept
having count(id)>2;

/*

display dept wise max salary, display only the dept which has max salary lessthan 50000

*/

select dept,max(salary) from employee

group by dept

having max(salary)>70000;

/***DDL****/

create table product

id int ,

name varchar(30),

description varchar(30),

price float,

expirydate date

);

/***table structure***/

describe product;

desc product;

insert into products values(4,'laptop','hp laptop',94000.00,'2024-1-4','good');

select * from product;

alter table product add (rating varchar(30));


alter table product drop rating;

alter table product modify rating varchar(5);

alter table product rename column rating to productrating;

rename table product to products;

select * from products;

delete from products where id>3;

truncate table products;

drop table products;

/*sql constarint --column level constraint*/

create table product1

id int primary key,

name varchar(30),

price float

);

/* table level constarint **/

create table product2

id int,

name varchar(30),

price float,

primary key(id)

);
drop table employee2;

create table employee2

id int primary key,

name varchar(30) not null,

dept varchar(30) default 'bench',

age int check(age>22),

emailid varchar(30) unique,

mobileno varchar(30) unique

);

desc employee2;

insert into employee2 values(1,'rahul','hr',24,'[email protected]','979897879');

insert into employee2 values(2,'kumar','hr',24,'[email protected]','779897879');

insert into employee2 values(3,'rahul','',24,'[email protected]','9899897879');

insert into employee2(id,name,age,emailid,mobileno)


values(4,'sourab',24,'[email protected]','989989787');

select * from employee2;

select * from employee;

desc employee;

alter table employee

add primary key(id);


create table emp1

id int primary key,

name varchar(30)

);

insert into emp1 values(2,'john');

create table emp2

id int primary key,

firstname varchar(30) not null,

lastname varchar(30)

);

insert into emp2 values(1,'rahul','dravid');

insert into emp2(id,firstname) values(2,'sachin');

insert into emp2(id,lastname) values(3,'sharma');

create table customer

id int primary key,

name varchar(30) not null,

email varchar(30) unique,

membership varchar(30) default 'not a member',

age int check(age>12 && age<50)

);

desc customer;

insert into customer values(1,'sachin','[email protected]','gold',3);


insert into customer(id,name,email,age) values(2,'mahesh','[email protected]',34);

insert into customer(id,name,email,age) values(3,'ankit','[email protected]',34);

insert into customer(id,name,age) values(6,'gopal',36);

select * from customer;

insert into customer(name,age) values('ramesh',40);

create table employee20

id int primary key,

name varchar(30),

dept varchar(30),

designation varchar(30),

salary float,

doj date

);

insert into employee20 values(3,'john','developer','trainee',84000,'2023/6/8');

create table employeepfdetail

pfid int primary key,

aadharNo int unique,

panNo int unique,

empid int,

foreign key(empid) references employee20(id)

);

desc employee20;
desc employeepfdetail;

select * from employee20;

insert into employeepfdetail values(1002,5544,67896,2);

select * from employeepfdetail;

delete from employee20 where id = 1;

/**foreign key delete*/

create table product

id int primary key,

name varchar(30),

description varchar(30)

);

create table productdetails

detailsid int primary key,

quantity int,

supplier varchar(30),

pid int,

foreign key(pid) references product(id) on delete set null);

insert into product values(3,'headphone','boat headphone');

insert into productdetails value(103,40,'xyz dealer',3);

select * from product;

select * from productdetails;


delete from product where id=2;

select * from employee;

select * from employee where salary > 50000;

select * from employee where salary > (select salary from employee where name='mohit');

select * from employee where id = 1;

select * from employee where id in (1,2,7);

select * from employee;

/***multi row sub query****/

/**select all the employee whose salary is greather than all the tester**/

select * from employee where salary >all (select salary from employee where dept='tester');

/**select all the employee, whose salary is greather than any one tester**/

select * from employee where salary >any (select salary from employee where dept='tester');

select * from employee where salary >any (select salary from employee where dept='tester') and dept
<>'tester';

/**day 5**/

select * from employee;

select * from employee where salary between 63000 and 80000;

select * from employee where age between 30 and 40;

select * from employee where name like 's%';

select * from employee where name like 'sr%';


select * from employee where name like '_a%';

select * from employee where name like '%t';

select * from employee where name like '%a_';

select * from employee where name like '__h%';

select * from employee where name like '%a%';

/***sql joins***/

create table student1

id int,

name varchar(30),

dept varchar(30)

);

create table studentdetails

id int,

year int,

dob date,

email varchar(30)

);

insert into student1 values(5,'suraj','eee');

select * from student1;

select * from studentdetails;

insert into studentdetails values(9,3,'2008-11-21','[email protected]');

select * from student1


inner join

studentdetails

on student1.id=studentdetails.id;

select * from student1

left outer join

studentdetails

on student1.id=studentdetails.id;

select * from student1

right outer join

studentdetails

on student1.id=studentdetails.id;

select s1.id,s1.name,sd1.dob,sd1.email from student1 s1

inner join

studentdetails sd1

on s1.id=sd1.id;

select s1.id,s1.name,sd1.dob,sd1.email from student1 s1

right outer join

studentdetails sd1

on s1.id=sd1.id;

select s1.id,s1.name,sd1.dob,sd1.email from student1 s1

left outer join

studentdetails sd1

on s1.id=sd1.id;
select * from student1

cross join

studentdetails

You might also like