0% found this document useful (0 votes)
25 views5 pages

Sqlquery

This document contains SQL commands and queries used to create databases, tables, insert, update, delete and select data from tables. It also contains commands to describe and alter tables, set variables, and perform joins, aggregations and filtering on data.

Uploaded by

8080852090
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
25 views5 pages

Sqlquery

This document contains SQL commands and queries used to create databases, tables, insert, update, delete and select data from tables. It also contains commands to describe and alter tables, set variables, and perform joins, aggregations and filtering on data.

Uploaded by

8080852090
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 5

freeze row nd col view freeze panes

group ungroup data tab to hide row or col

home find&select go to blank to del empty row or col

ctrl shft ; current date

ctrl ; current date wont chng ever

show databases;
drop database Testdb;
use mysql;
select * from sys.host_summary;
use sys;
create database testdb;
use testdb;
create table employee(id int, name varchar(12), experience int);
describe employee;
create table emp as
select id,name from employee;
drop table emp;
create table anurag as
select file_ios, table_scans from sys.host_summary;
describe anurag;
use testdb;
describe employee;
insert into employee values(2, 'anurag', 5);
select * from employee;
insert into employee value((2, 'anurag', 5),
(9,'jgf', 8)
);
insert into employee(id,name) values(6,'gfh');
use testdb;
create table anu(id int);
describe anu;
select * from anu;
insert into anu values(1);
drop table anu;
create table anu(a double);
insert into anu values(9.8);
drop table anu;
create table anu(id boolean);
insert into anu value(true);
create table anu(a date);
insert into anu values('1234-12-3');
select * from anu where name is not null;
drop table anu;
create table anu(id int ,name varchar(67));
insert into anu(id) values(7);
insert into anu(id,name) values(7,'anurag');
select * from anu;
SET SQL_SAFE_UPDATES = 0;
delete from anu where name ='anurag';
select * from anu;
describe anu;
insert into anu values(7,'amit');
insert into anu values(9,'atul');
update anu set name='anishka' where name='atul';
rename table rag to anu;
select * from anu;
alter table anu add abc varchar(34);
alter table anu
rename column abc to cde;
insert into anu values(1,'anurag',8);
alter table anu
modify column cde varchar(34);
select * from anu;
update anu set name='amit' where id=7;
insert into anu values(4,'atul',3);
set autocommit=0;

CREATE DATABASE campusx;


show databases;
DROP DATABASE campusx;
CREATE DATABASE IF NOT exists campusx;
DROP DATABASE IF EXISTS campusx;
CREATE DATABASE IF NOT EXISTS campusx;
CREATE TABLE users(userid INTEGER,
name VARCHAR(255),
email VARCHAR(255),
password VARCHAR(255))
SELECT * from users;
INSERT INTO users(userid,name,email,password) VALUE(1,anu.shri,shri,asf)
TRUNCATE table users;
CREATE table users(userid INTEGER,
name VARCHAR(255),
email VARCHAR(255),
password VARCHAR(255))
DROP table users;
INSERT INTO users VALUES(2, 'anurag', 'anu@gmail.com', 'shri420');
INSERT INTO users VALUES(3,'amit', 'smiy@gmail.com','amit34')
DESCRIBE users;
SELECT * from users;
CREATE table employee(name VARCHAR(255) NOT NULL UNIQUE,
age INT NOT NULL,
email VARCHAR(255));
DESCRIBE details;
SELECT * from employee;
INSERT INTO employee VALUES('anurag', 45, 'anurag.shri@gmail.com');
INSERT INTO employee(name, email) VALUES('anurag', 'anurag.shri@gmail.com');
INSERT INTO employee(name, age) VALUES('anurag', 45)
DROP table employee;

USE testdb;
drop table details;
CREATE table details(name VARCHAR(255),
age INT,
rollno INT,
CONSTRAINT namepk PRIMARY KEY (name))
describe details;
INSERT INTO details(name, age, rollno) values('anurag', 23, 2345)
INSERT INTO details(name, age, rollno) value('anurag', 24, 345)
use testdb;
CREATE TABLE anurag(userid int PRIMARY KEY AUTO_INCREMENT,
name varchar(255),
age int);
INSERT INTO anurag(name, age) VALUES('amit', 34);
INSERT INTO anurag(name, age) VALUES('am', 3);
describe anurag;
select * from anurag;
drop table anurag;

CREATE TABLE anurag(userid int,


name VARCHAR(255),
age int,
CONSTRAINT age_check CHECK (age>8 AND age <78));
INSERT INTO anurag VALUES(1, 'ANU', 4);
DROP TABLE anurag;
CREATE TABLE ticket(ticketid INTEGER PRIMARY KEY,
name varchar(255) NOT NULL,
travel_date DATETIME DEFAULT CURRENT_TIMESTAMP);
INSERT INTO ticket(ticketid, name, travel_date) VALUES(1, 'ANURAG', '02/02/1223');
CREATE TABLE customers(cid INTEGER PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE);

CREATE TABLE orders(orderid INTEGER PRIMARY KEY AUTO_INCREMENT,


cid INTEGER NOT NULL,
orderdate DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT ordersfk FOREIGN KEY (cid) REFERENCES customers(cid));

INSERT INTO orders(cid) VALUES(2);


DROP table customers;
INSERT INTO customers(cid, name, email) VALUES(1, 'ANURAG','anurag@gmail.com')
select * from anurag;
ALTER TABLE ticket ADD column surname VARCHAR(255) AFTER name;
select * from ticket;
ALTER TABLE ticket DROP COLUMN surname;
ALTER TABLE ticket MODIFY COLUMN name INT;
ALTER TABLE ticket ADD COLUMN age int;
INSERT INTO ticket VALUES(8,90,'02-02-2022',6);
CREATE TABLE emp(name VARCHAR(255) DEFAULT 'ANU',
age INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT);
INSERT INTO emp(age) VALUES(34);
ALTER TABLE emp ADD COLUMN email varchar(255);
insert into emp(email) values('anu@gmail.com');
select * from emp;
CREATE DATABASE campusx;
DROP DATABASE campusx;
USE campusx;
CREATE TABLE users(usersid INTEGER PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL);
INSERT INTO users VALUES(NULL,'anurag','anu@gmail.com','1234');
SELECT model AS ml, price AS priceinrupee from smartphones;
SELECT model,
SQRT(resolution_width*resolution_width+resolution_height*resolution_height)/
screen_size AS PPI FROM smartphones;
SELECT * FROM smartphones;
use campusx;
SELECT DISTINCT(brand_name) from smartphones;
SELECT DISTINCT brand_name, processor_brand FROM smartphones;
SELECT * FROM smartphones WHERE price >50000 AND brand_name='samsung';
SELECT distinct(brand_name) FROM smartphones where price>50000;
SELECT brand_name from smartphones where brand_name='apple' or brand_name='samsung'
SELECT brand_name from smartphones where brand_name in ('apple','samsung');
update smartphones set brand_name='sams' where brand_name='samsung'
update smartphones set brand_name='samsu' where brand_name='samsung'
select * from smartphones where price > 200000;
delete from smartphones where price>200000;
select count(distinct(brand_name)) from smartphones;
select * from smartphones where brand_name='sams' order by screen_size desc limit
5;
select num_rear_cameras+num_front_cameras, model as total_camera from smartphones
order by total_camera desc limit 3
select * from smartphones;
select battery_capacity as power from smartphones order by power desc limit 1,1
select model from smartphones where battery_capacity=22000;
select rating,model from smartphones where brand_name='apple' order by rating asc
limit 1
select rating from smartphones where brand_name='apple' order by rating asc
select price, model from smartphones order by brand_name asc, price asc
select brand_name, count(*), avg(price), max(rating) from smartphones
group by brand_name order by count(*) desc;
select has_nfc, avg(price), round(avg(rating),2) from smartphones group by has_nfc
select has_nfc, has_5g, brand_name from smartphones group by has_nfc, has_5g,
brand_name;
select brand_name, processor_brand, count(model),
round(avg(primary_camera_rear),1) from smartphones
group by brand_name, processor_brand;
select avg(price),max(price), brand_name from smartphones group by brand_name order
by avg(price) desc limit 5;
select brand_name, screen_size from smartphones order by screen_size asc limit 1;
select * from smartphones;
select brand_name, count(brand_name) from smartphones
where has_ir_blaster='True' and has_nfc='True' group by brand_name;
select avg(price), has_nfc from smartphones where brand_name='sams' group by
has_nfc;
select model from smartphones where price=179900;
select model, price, brand_name from smartphones order by price desc limit 1;
select brand_name, avg(price), count(*) from smartphones
group by brand_name having count(*)>20
order by avg(price) desc;
select avg(rating), brand_name from smartphones
group by brand_name having avg(rating)>70
order by avg(rating) desc;
select * from smartphones;
select brand_name, avg(ram_capacity), fast_charging_available, refresh_rate from
smartphones
where refresh_rate>90 and fast_charging_available=1
group by brand_name having count(*)>10 order by avg(ram_capacity) desc limit 3;
select avg(price), brand_name from smartphones where has_5g='True' group by
brand_name having avg(rating)>70

use testdb;
select * from users1;
select * from membership;
drop table users;
select * from testdb.groups
select * from groups
select * from users1 t1 cross join testdb.groups t2;
select * from membership t1 inner join users1 t2 on t1.user_id=t2.user_id;
select * from membership t1 left join users1 t2 on t1.user_id=t2.user_id;
select * from membership t1 right join users1 t2 on t1.user_id=t2.user_id;
select * from person1 intersect;
select * from person2;
select * from users1 u1 inner join users1 u2 on u1.emergency_contact;
select * from class c inner join students s on c.class_id= s.class_id and
c.class_year=s.enrollment_year;
select * from class;
select * from students;
select * from testdb.users;
select * from orders;
select * from order_details;
select * from category;
select * from users;
select * from orders o join order_details od on o.order_id=od.order_id join users
t3 on o.user_id=t3.user_id
use testdb;
select o.order_id, u.name, u.city from orders o join users u on
o.user_id=u.user_id;
select order_id, vertical from order_details o join category c on o.category_id =
c.category_id
select * from users u join orders o on u.user_id=o.user_id where city='pune';
select * from category c join order_details o on c.category_id=o.category_id where
vertical='chair';
select o.order_id,sum(profit) from orders o join order_details od
on o.order_id=od.order_id group by o.order_id having sum(profit)>0;
select name, count(*) from users u join orders o on u.user_id=o.user_id group by
name
use testdb;
select * from movies;

You might also like