Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- https://g.co/gemini/share/064c1e56fe34
- show databases;
- create database experiment;
- use experiment;
- create table student(ID int,Name varchar(30),Age int);
- alter table student add Grade varchar(3);
- desc student;
- insert into student (ID,Name,Age,Grade) values(101,'John Doe',25,'A'),(201,'Willliam Turber',30,'O'),(301,'Olivia',19,'B'),(401,'Alice Brown',21,'A'),(501,'Sheldon Cooper',20,'S');
- select * from student;
- set sql_safe_updates=0;
- update student set age=21 where ID=101;
- delete from student where id=101;
- select * from student;
- select * from student where age>=19;
- select * from student where Name='Willliam Turber' or Name='Alice Brown';
- select * from student where Name in ('Willliam Turber','Alice Brown');
- select * from student;
- select * from student where Grade in ('A','B') and age>=20;
- select * from student where Age between 20 and 25;
- select * from student where Age<20;
- select * from student where Grade>'B';
- create table employee(id int,firstname varchar(100),lastname varchar(100),address varchar(100),city varchar(100));
- insert into employee(id,firstname,lastname,address,city) values(101,'John','Kumar','Lane 1','Bengaluru');
- insert into employee(id,firstname,lastname,address,city) values(102,'King','Singh','Lane 2','Bengaluru');
- insert into employee(id,firstname,lastname,address,city) values(103,'Ben','Sharma','Lane 3','Bengaluru');
- select * from employee;
- alter table employee add column salary int;
- select * from employee;
- set sql_safe_updates = 0;
- update employee set salary = case when id=101 then 10000 when id=102 then 20000 when id=103 then 30000 else salary end;
- select * from employee;
- alter table employee drop column salary;
- select * from employee;
- alter table employee rename column city to jagah;
- select * from employee;
- rename table employee to gulaam;
- select * from gulaam;
- create table student(snum integer,sname varchar(10),major varchar(2),level varchar(2),age integer,PRIMARY KEY(snum));
- create table faculty(fid integer,fname varchar(20),deptid integer,PRIMARY KEY(fid));
- create table class(cname varchar(20),meets_at varchar(10),room varchar(10),fid integer,PRIMARY KEY(cname),FOREIGN KEY(fid) REFERENCES faculty(fid));
- create table enrolled(snum integer,cname varchar(20),PRIMARY KEY(snum,cname),FOREIGN KEY(snum) REFERENCES student(snum),FOREIGN KEY(cname) REFERENCES class(cname));
- insert into student(snum,sname,major,level,age) values
- (1,'jhon','CS','Sr',19),(2,'smith','CS','Jr',20),(3,'jacob','CV','Sr',20),(4,'tom','CS','Jr',20),(5,'sid','CS','Jr',20),(6,'harry','CS','Sr',21);
- insert into faculty(fid,fname,deptid) values(11,'Harshith',1000),(12,'Mohan',1000),(13,'Kumar',1001),(14,'Shobha',1002),(15,'Shan',1000);
- insert into student(snum,sname,major,level,age) values(7,'Kala','AI','Sr',21);
- insert into class(cname,meets_at,room,fid) values
- ('class1','noon','room1',14),('class10','morning','room128',14),
- ('class2','morning','room2',12),('class3','morning','room3',11),
- ('class4','evening','room4',14),('class5','night','room3',15),
- ('class6','morning','room2',14),('class7','morning','room3',14);
- insert into enrolled(snum,cname) values
- (1,'class1'),(2,'class1'),(3,'class3'),(4,'class3'),(3,'class4'),(5,'class4'),
- (1,'class5'),(2,'class5'),(3,'class5'),(4,'class5'),(5,'class5'),(6,'class5');
- select * from student;
- select * from class;
- select * from enrolled;
- select * from faculty;
- select distinct s.sname from student s,class c,faculty f,enrolled e where s.snum=e.snum and e.cname=c.cname and s.level='Jr' and f.fname='Harshith' and f.fid=c.fid;
- select distinct cname from class where room='room128' or cname in (select e.cname from enrolled e group by e.cname having COUNT(*)>=5);
- INSERT INTO enrolled (snum,cname) VALUES (3,'class2');
- SELECT DISTINCT s.sname FROM student s WHERE s.snum IN (SELECT el.snum FROM enrolled el,enrolled e2,class c1,class c2 WHERE el.snum=e2.snum AND el.cname=c1.cname AND e2.cname=c2.cname AND c1.meets_at=c2.meets_at);
- select f.fname, f.fid from faculty f where f.fid in (select fid from class group by fid having count(*)=(select count(distinct room) from class));
- select f.fname from faculty f WHERE 5>(select count(e.snum) from class c, enrolled e where c.cname=e.cname and c.fid=f.fid);
- create table flight(flno integer,frm varchar(20),too varchar(20),distance integer,departs varchar(20),arrives varchar(20),price integer,PRIMARY KEY(flno));
- create table aircraft(aid integer,aname varchar(20),cruisingrange integer,PRIMARY KEY(aid));
- create table employees(eid integer,ename varchar(20),salary integer,PRIMARY KEY(eid));
- create table certified(eid integer,aid integer,PRIMARY KEY(eid,aid),FOREIGN KEY(eid) REFERENCES employees(eid),FOREIGN KEY(aid) REFERENCES aircraft(aid));
- insert into flight(flno, frm, too, distance, departs, arrives,price) values
- (1, 'Bangalore', 'Mangalore', 360, '10:45:00', '12:00:00',10000),
- (2,'Bangalore', 'Delhi', 5000,'12:15:00','04:30:00',25000),
- (3,'Bangalore', 'Mumbai', 3500,'02:15:00','05:25:00',30000),
- (4,'Delhi', 'Mumbai', 4500,'10:15:00', '12:05:00',35000),
- (5,'Delhi', 'Frankfurt', 18000,'07:15:00','05:30:00',90000),
- (6,'Bangalore', 'Frankfurt', 19500,'10:00:00','07:45:00',95000),
- (7,'Bangalore', 'Frankfurt', 17000,'12:00:00','06:30:00',99000);
- insert into aircraft(aid,aname,cruisingrange) values
- (123,'Airbus', 1000),(302,'Boeing', 5000),(306,'Jet01',5000),(378,'Airbus380',8000),
- (456,'Aircraft',500),(789,'Aircraft02',800),(951,'Aircraft03',1000);
- insert into employees(eid,ename,salary) values
- (1,'Ajay',30000),(2,'Ajith',85000),(3,'Arnab', 50000),(4,'Harry', 45000),
- (5,'Ron',90000),(6,'Josh', 75000),(7,'Ram', 100000);
- insert into certified(eid,aid) values
- (1,123),(2,123),(1,302),(5,302),(7,302),(1,306),(2,306),(1,378),(2,378),(4,378),(6,456),(3,456),(5,789),(6,789),(3,951),(1,951),(1,789);
- select * from employees;
- select * from aircraft;
- select * from flight;
- select * from certified;
- select distinct a.aname from aircraft a, certified c, employees e where a.aid=c.aid and c.eid=e.eid and e.salary>80000;
- SELECT DISTINCT a.aname FROM aircraft a, certified c, employees e WHERE a.aid=c.aid AND c.eid=e.eid AND NOT EXISTS (SELECT * FROM employees e1 WHERE e1.eid=e.eid AND e1.salary<80000);
- SELECT c.eid,MAX(cruisingrange) FROM certified c,aircraft a WHERE c.aid=a.aid GROUP BY c.eid HAVING COUNT(*)>3;
- SELECT DISTINCT e.ename FROM employees e WHERE e.salary< (SELECT MIN(f.price) FROM flight f WHERE f.frm='Bangalore' AND f.too='Frankfurt');
- SELECT a.aname, AVG(e.salary) FROM aircraft a,certified c,employees e WHERE a.aid=c.aid AND c.eid=e.eid AND a.cruisingrange>1000 GROUP BY a.aname;
- SELECT distinct e.ename FROM employees e,aircraft a,certified c WHERE e.eid=c.eid AND c.aid=a.aid AND a.aname='Boeing';
- SELECT a.aid FROM aircraft a WHERE a.cruisingrange>(SELECT MIN(f.distance) FROM flight f WHERE f.frm='Bangalore' AND f.too='Delhi');
- create table customer(cust int,cname varchar(10), city varchar(10), primary key(cust));
- create table order1(ord int, odate date,cust int,ord_amt int,primary key(ord));
- create table item(item int, unit_price int, primary key(item));
- create table order_item(ord int REFERENCES order1(ord), item int REFERENCES item(item) on delete set null, qty int);
- create table warehouse(warehouse int, city varchar(15), primary key(warehouse));
- create table shipment(ord int REFERENCES order1(ord), warehouse int REFERENCES warehouse(warehouse), ship_date date);
- insert into customer(cust,cname,city) values
- (2,'shashi','N.America'),(3,'kala','U.S.A'),(5,'Dumini','India'),(2002,'Stuart','Switz'),
- (2003,'Binny','N.America'),(2004,'Curie','U.S.A'),(2005,'Dumini','India'),(2006,'Rick','India');
- insert into item(item,unit_price) values
- (1500,250),(750,167),(125,124),(136,420),(250,138),(10,500);
- insert into shipment(ord,warehouse,ship_date) values
- (1,322,'4-03-21'),(2,234,'7-12-23'),(3,324,'14-06-24'),
- (4,325,'12-10-25'),(5,326,'12-4-25');
- insert into order1(ord,odate,cust,ord_amt) values
- (1,'2006-10-5',2,5000),(2,'2006-10-8',3,2500),(3,'2006-10-9',3,1000),
- (4,'2006-10-6',5,1000),(5,'2006-11-3',1,5000);
- insert into order_item(ord,item,qty) values
- (1,1500,1),(1,750,1),(2,125,1),(3,136,5),(4,250,2),(4,10,2);
- insert into warehouse(warehouse,city) values
- (321,'Bangalore'),(322,'Hyderabad'),(323,'Bangalore'),(324,'New Delhi'),(325,'New York');
- select c.cname, count(*) as no_of_orders, avg(o.ord_amt) as avg_order_amt from customer c,order1 o where c.cust=o.cust group by c.cname;
- select ord from warehouse w,shipment s where w.warehouse=s.warehouse and city="New Delhi";
- delete from item where item=250;
- select * from order_item;
- Create table author (
- author_id int primary key,
- name varchar(50),
- city varchar(50),
- country varchar(50)
- );
- Create table publisher (
- publisher_id int primary key,
- name varchar(50),
- city varchar(50),
- country varchar(50)
- );
- Create table category (
- category_id int primary key,
- description varchar(100)
- );
- Create table catalog (
- book_id int primary key,
- title varchar(100),
- author_id int,
- publisher_id int,
- category_id int,
- year int,
- price int,
- foreign key (author_id) references author(author_id),
- foreign key (publisher_id) references publisher(publisher_id),
- foreign key (category_id) references category(category_id)
- );
- Create table order_details (
- order_no int,
- book_id int,
- quantity int,
- primary key (order_no, book_id),
- foreign key (book_id) references catalog(book_id)
- );
- insert into author (author_id, name, city, country) values
- (1, 'Author A', 'City 1', 'Country 1'),
- (2, 'Author B', 'City 2', 'Country 2'),
- (3, 'Author C', 'City 3', 'Country 3'),
- (4, 'Author D', 'City 4', 'Country 4'),
- (5, 'Author E', 'City 5', 'Country 5');
- insert into publisher (publisher_id, name, city, country) values
- (1, 'Publisher A', 'City 1', 'Country 1'),
- (2, 'Publisher B', 'City 2', 'Country 2'),
- (3, 'Publisher C', 'City 3', 'Country 3'),
- (4, 'Publisher D', 'City 4', 'Country 4'),
- (5, 'Publisher E', 'City 5', 'Country 5');
- insert into category (category_id, description) values
- (1, 'Fiction'),(2, 'Non-Fiction'),(3, 'Science Fiction'),
- (4, 'Mystery'),(5, 'Thriller');
- insert into catalog (book_id, title, author_id, publisher_id, category_id, year, price) values
- (1, 'Book 1', 1, 1, 1, 2005, 100),(2, 'Book 2', 2, 1, 2, 2010, 120),
- (3, 'Book 3', 3, 3, 3, 2015, 150),(4, 'Book 4', 4, 4, 4, 2020, 200),
- (5, 'Book 5', 5, 5, 5, 2025, 250);
- insert into order_details (order_no, book_id, quantity)
- values (101, 1, 2),(102, 2, 3),
- (103, 3, 1),(104, 4, 4),(105, 5, 2);
- select a.author_id, a.name, a.city, a.country, count(c.book_id) as book_count from author a
- join catalog c on a.author_id = c.author_id where c.year > 2000
- group by a.author_id, a.name, a.city, a.country having count(c.book_id) >= 2;
- select a.author_id, a.name, a.city, a.country, c.title, od.quantity from order_details od
- join catalog c on od.book_id = c.book_id join author a on c.author_id = a.author_id
- where od.quantity = (select max(quantity) from order_details);
- update catalog set price = price * 1.10 where
- publisher_id = (select publisher_id from publisher where name = 'Publisher E');
- select * from catalog;
- create table student (regno varchar(15), name varchar(20), major varchar(20), bdate date, primary key (regno));
- insert into student (regno, name, major, bdate) values
- ('Inh21ai001', 'a', 'sr', '19930120'),('Inh21ai002', 'b', 'sr', '19930924'),
- ('Inh21ai003', 'c', 'sr', '19931127'),('Inh21ai004', 'd', 'sr', '19930413'),('Inh21ai005', 'e', 'isr', '19940824');
- create table course (courseno int, cname varchar(20), dept varchar(20), primary key (courseno));
- insert into course values (111, 'DS', 'CSE'),(112, 'EC', 'CSE'),
- (113, 'SS', 'ISE'),(114, 'DBMS', 'CSE'),(115, 'SSANNIS', 'ECE'),
- (121, 'OS', 'AIML'),(123, 'SS', 'AIML'),(124, 'DBMS', 'AIML');
- create table text (book_isbn varchar(20), book_title varchar(20), publisher varchar(20), author varchar(20), primary key (book_isbn));
- insert into text (book_isbn, book_title, publisher, author) values
- ('900', 'Database Systems', 'Pearson', 'Schield'),
- ('901', 'Operating Sys', 'Pearson', 'Leland'),
- ('902', 'Circuits', 'Hall India', 'Bob'),
- ('903', 'System Software', 'Peterson', 'Jacob'),
- ('904', 'Scheduling', 'Pearson', 'Patil'),
- ('905', 'Database Systems', 'Pearson', 'Jacob'),
- ('906', 'Database Manager', 'Pearson', 'Bob'),
- ('907', 'Signali', 'Hall India', 'Sumit');
- create table enroll (regno varchar(15), courseno int, sem int, marks int, primary key (regno, courseno), foreign key (regno) references student (regno), foreign key (courseno) references course (courseno));
- insert into enroll (regno, courseno, sem, marks) values
- ('Inh21ai001', 114, 3, 100),('Inh21ai002', 115, 3, 100),
- ('Inh21ai003', 113, 5, 100),('Inh21ai004', 111, 5, 100),('Inh21ai005', 112, 3, 100);
- create table book_adoption (courseno int, sem int(3), book_isbn varchar(20), primary key (courseno, sem, book_isbn), foreign key (courseno) references course (courseno), foreign key (book_isbn) references text (book_isbn));
- insert into book_adoption (courseno, sem, book_isbn) values
- (111, 5, '900'),(111, 3, '903'),(114, 5, '904'),(113, 5, '902'),
- (115, 3, '906'),(121, 5, '903'),(121, 5, '904'),(121, 5, '905');
- select c.courseno, t.book_isbn, t.book_title from course c, book_adoption ba, text t where c.courseno = ba.courseno and ba.book_isbn = t.book_isbn and c.dept = 'AIML' and 2 <= (select count(book_isbn) from book_adoption b where c.courseno = b.courseno) order by t.book_title;
- select distinct c.dept from course c where c.dept in (select c.dept from course c, book_adoption b, text t where c.courseno = b.courseno and b.book_isbn = t.book_isbn and t.publisher = 'Pearson') and c.dept not in (select c.dept from course c, book_adoption b, text t where c.courseno = b.courseno and b.book_isbn = t.book_isbn and t.publisher != 'Pearson');
- create table actor (act_id integer primary key, act_name varchar(20), act_gender char(1));
- create table director (dir_id integer primary key, dir_name varchar(20), dir_phone varchar(20));
- create table movies (mov_id integer primary key, mov_title varchar(25), mov_year integer, mov_lang varchar(15), dir_id integer, foreign key (dir_id) references director (dir_id));
- create table movie_cast (act_id integer, mov_id integer, role varchar(10), primary key (act_id, mov_id), foreign key (act_id) references actor (act_id), foreign key (mov_id) references movies (mov_id));
- create table rating (mov_id integer primary key, rev_stars varchar(25), foreign key (mov_id) references movies (mov_id));
- insert into actor (act_id, act_name, act_gender) values
- (101, 'Rahul', 'M'),(102, 'Ankitha', 'F'),(103, 'Radhika', 'F'),
- (104, 'Chethan', 'M'),(105, 'Vivan', 'M');
- insert into director (dir_id, dir_name, dir_phone) values
- (201, 'Anoop', '9181818181'),(202, 'Hitchcock', '9181818182'),
- (203, 'Shashank', '9181818183'),(204, 'Steven Spielberg', '9181818184'),
- (205, 'Anand', '9181818185');
- insert into movies (mov_id, mov_title, mov_year, mov_lang, dir_id) values
- (1001, 'MANASU', 2017, 'Kannada', 201),(1002, 'AAKASHAM', 2015, 'Telugu', 202),
- (1003, 'KALIYONA', 2008, 'Kannada', 201),(1004, 'WAR HORSE', 2011, 'English', 204),
- (1005, 'HOME', 2012, 'English', 205);
- insert into movie_cast (act_id, mov_id, role) values
- (101, 1002, 'Hero'),(101, 1001, 'Hero'),(103, 1003, 'Heroine'),
- (103, 1002, 'Count'),(104, 1004, 'Hero'),(105, 1005, 'Hero');
- insert into rating (mov_id, rev_stars) values
- (1001, 4),(1002, 3),(1003, 5),(1004, 4),(1005, 3);
- select mov_title from movies where dir_id = (select dir_id from director where dir_name = 'Hitchcock');
- select m.mov_title from movies m join movie_cast mc on m.mov_id = mc.mov_id group by m.mov_id having count(mc.act_id) >= 2;
- select act_name from actor a join movie_cast mc on a.act_id = mc.act_id join movies m on mc.mov_id = m.mov_id where m.mov_year not between 2000 and 2015;
- select m.mov_title, max(r.rev_stars) from movies m inner join rating r using (mov_id) group by m.mov_title having max(r.rev_stars) > 0 order by m.mov_title;
- update rating set rev_stars = 5 where mov_id in (select mov_id from movies where dir_id in (select dir_id from director where dir_name = 'Steven Spielberg'));
- select * from rating;
- create table employee(e_id int primary key,e_name varchar(50),age int,salary int);
- insert into employee(e_id, e_name, age, salary) values
- (1, 'Ani', 30, 50000),
- (2, 'Bob', 25, 60000),
- (3, 'Charlie', 35, 55000),
- (4, 'David', 28, 50000),
- (5, 'Eva', 30, 70000);
- select count(e_name) as Total_Employees from employee;
- select max(age) as Max_Age from employee;
- select min(age) as Min_Age from employee;
- select e_name, salary from employee order by salary asc;
- select salary, count(*) as Num_Of_Employees from employee group by salary;
Add Comment
Please, Sign In to add comment