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

SQL Commands

The document contains SQL commands for creating a student table with fields like ID, name, email and phone number. It inserts sample data, performs queries to retrieve, update and delete data using operators, joins, aggregation functions, sorting and more. Various SQL concepts like creating/describing tables, inserting data, selecting data using where/order by/limit clauses, updating, deleting, counting, finding average, minimum, maximum values and more are demonstrated.

Uploaded by

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

SQL Commands

The document contains SQL commands for creating a student table with fields like ID, name, email and phone number. It inserts sample data, performs queries to retrieve, update and delete data using operators, joins, aggregation functions, sorting and more. Various SQL concepts like creating/describing tables, inserting data, selecting data using where/order by/limit clauses, updating, deleting, counting, finding average, minimum, maximum values and more are demonstrated.

Uploaded by

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

Create table-

create table students ( id int Primary Key, name varchar(32) not null, email
varchar(32), phoneNumber long);--

describe table-
desc students;

insert into table-


insert into students value ( 4, "rashmi", "[email protected]" , "9971866787");

insert into students (id, name , email, phoneNumber)


value (1, "sarvesh", "[email protected]" , "8826107920");

insert into students (id, name , email, phoneNumber)


values ( 2, "krishan", "[email protected]" , "9810355392"), ( 3, "pinki", "[email protected]" ,
"9971867787");

get data-
select * from students;
select name, email from students;
select * from students where email = ("[email protected]");

Operator-
select * from students where id > 1;
select * from students where id = 1;
select * from students where id < 3;
select * from students where id <= 3;

Maths operators-
SELECT english + maths from marksheet;
SELECT sid, english + maths from marksheet;
SELECT sid, english + maths + science as total_marks from marksheet;
SELECT sid, 300 - (english + maths + science) as remaining_marks from marksheet;
SELECT sid, ((english + maths + science)/300) * 100 as percentage from marksheet;
SELECT sid, ((english + maths + science)/3) as average from marksheet;

Logical operators-
SELECT english > 70 && maths >70 from marksheet;
SELECT english >=12 && science <=87 from marksheet;
SELECT sid >=2 and science <=87 from marksheet;

UPDATE TABLE-
if you are getting an error- "you are using safew update mode" then type - set
sql_safe_updates = 0;
update {table_name} set {column_name} = {new_value} where {condition};
UPDATE marksheet set english = 58 where sid = 1;
UPDATE students set email = "[email protected]" , name = "sarvesh_g:" where id = 1;

DELETE TABLE-
if you are getting an error- "you are using safew update mode" then type - set
sql_safe_updates = 0;
Delete from students where email = "[email protected]" , name = "sarvesh_g:" where
id = 1;

COUNT-
SELECT COUNT(*) from marksheet WHERE maths > avg(maths);

avg-
SELECT avg(maths) from marksheet;

order-
assending ( default)
select * from marksheet order by maths;
or
select * from marksheet order by maths asc;

descending
select * from marksheet order by maths desc;

LIKE-
Select * from students where name like "regex code";
start frrom a specfic word
Select * from students where name like "ra%";
must have letter
Select * from students where name like "%r%";
end with a specific letter
Select * from students where name like "%i";

min max-
Select min(english) from marksheet;
Select max(english) from marksheet;

sum-
Select sum(english) from marksheet;

distinct-
Select distinct(english) from marksheet;

having filters the data after aggregation which happens by group by command, and
Where filters the data at the first

GRoup-
select count(*), brand from cars group by brand;
select count(*), brand from cars group by brand having count(*) >=2;
select count(*), brand from cars group by brand having count(*) >=2 order by brand;

multiplication of tables-
select * from students, marksheet;

three type of joins-


join- (inner join default) - select the data common in both tables
select * from students join marksheet on students.id = marksheet.sid;
select name, english+maths+science from students join marksheet on students.id =
marksheet.sid;
select name, english+maths+science as total_marks from students join marksheet on
students.id = marksheet.sid;

left join- - select the data common in both tables + all elements from left table
select * from students left join marksheet on students.id = marksheet.sid;

right join- - select the data common in both tables + all elements from right
table
select * from students right join marksheet on students.id = marksheet.sid;

full join- - select the data common in both tables + all elements from left table
+ all elements from right table
full join does not work with mysql but work with other sql services
select * from students full join marksheet on students.id = marksheet.sid;

union- joins two outputs


conditions-
1. no of columns in both outputs must be same
2. data types of columns in both outputs must be same
ex1 - we have two tables 1 with male persons data and other with femlae persons
data, and we want no of male permonsa nd femlae persons in one output
so here we will use union to first find count of male from first table and female
from second table then join both output by union
select count(*),gender from female_persons union select count(*),gender from
male_persons ;

ex1 - we have two tables c1 and c2 with both male and female persons data, and we
want no of male permons nd femlae persons in both companies in one output
so here we will use group to first find count of male and female from c1 table then
from c2 and then join both output by union
select count(*),gender from c1 group by gender union select count(*),gender from c1
group by gender;

subqueries-
print all details of studnet wiht max marks in maths
select * from marksheet where maths = (select max(maths) from marksheet);
select sid from marksheet where maths = (select max(maths) from marksheet);
select * from marksheet where english < (select max(english) from marksheet) order
by english desc limit 1;

offset- skips rows


select * from worker order by salary desc limit 1 offset 2;

You might also like