You are on page 1of 2

SQL:

show databases;
create database db18;
use db18;
create table emp (empid int,empname varchar(20),empsalary int, empcity
varchar(20));
show tables;
desc emp;
insert into emp values(100,'Ram','10000','Indore');
insert into emp values(101,'Shyam','12000','Indore'),(102,'Raj','15000','Bhopal');
insert into emp(empid,empname,empcity) values(103,'John','Rau');
SELECT empid,empname,empsalary,empcity from emp;
SELECT * from emp;
SELECT empsalary from emp where empname='ram';
SELECT * from emp where empsalary>10000;
SELECT * from emp where empsalary<10000;
SELECT * from emp where empsalary=10000;
SELECT * from emp where empsalary!=10000;
SELECT * from emp where empsalary<>10000;
SELECT * from emp where empsalary=NULL;
SELECT * from emp where empsalary is NULL;
SELECT * from emp where empsalary>10000 AND empcity='Indore';
SELECT * from emp where empsalary>10000 OR empcity='Indore';
SELECT * from emp where empsalary>=10000 AND empsalary<=15000;
SELECT * from emp where empsalary between 10000 AND 15000;
SELECT * from emp where empname like 'R%';
SELECT * from emp where empname like '%m';
SELECT * from emp where empname like '%a%';
SELECT * from emp where empname like '_a%';
SELECT * from emp where empname like '%a_';
SELECT (2+2);
SELECT (2+2) as Total;
SELECT empid,empname,empsalary,empcity from emp;
SELECT empid,empname,empsalary,empcity as 'Employee ka shahar' from emp;
SELECT empid,empname,empsalary,empsalary+1000 as 'Salary with bonus',empcity from
emp;

SUM()
SELECT SUM(empsalary) from emp;

MIN
SELECT MIN(empsalary) from emp;
SELECT * from emp where empsalary=(SELECT MIN(empsalary) from emp);

MAX
SELECT MAX(empsalary) from emp;
SELECT * from emp where empsalary=(SELECT MAX(empsalary) from emp);
SELECT * from emp where empsalary=(SELECT MAX(empsalary) from emp where
empsalary<(SELECT MAX(empsalary) from emp));

AVG
SELECT avg(empsalary) from emp;

COUNT
SELECT count(empsalary) from emp;
SELECT count(*) from emp;
UPDATE emp set empsalary=5000 where empid=103;
SELECT * from emp limit 0,2;
SELECt * FROM emp order by empsalary;
SELECt * FROM emp order by empsalary desc;

SELECT empcity from emp;


SELECT DISTINCT(empcity) from emp;

SELECt * FROM emp order by empsalary desc limit 0,1;


SELECT DISTINCT(empsalary) from emp;
SELECT DISTINCT(empsalary) from emp order by empsalary;
SELECT DISTINCT(empsalary) from emp order by empsalary desc;
SELECT DISTINCT(empsalary) from emp order by empsalary desc limit 0,1;
SELECT * from emp where empsalary = (SELECT DISTINCT(empsalary) from emp order by
empsalary desc limit 0,1);

ALTER TABLE emp add column mailid int after empname;


ALTER TABLE emp add column contact varchar(13) FIRST;
ALTER TABLE emp CHANGE mailid empmailid varchar(35);
ALTER TABLE emp CHANGE contact empcontact varchar(13);
ALTER TABLE emp DROP column empmailid,drop column empcontact;

delete from emp where empid=106;


Select empsalary,count(empsalary) from emp group by empsalary;
Select empcity,count(empcity) from emp group by empcity;

CREATE TABLE empcopy as (SELECT empid,empname,empcity from emp);


CREATE VIEW empview as (SELECT empid,empname,empcity from emp);
UPDATE empcopy set empcity='Pithampur' where empid=103;
UPDATE empview set empcity='Rangwasa' where empid=103;
delete from empcopy;
DROP TABLE empcopy;
DROP VIEW empview;

You might also like