Professional Documents
Culture Documents
Class 1 SQL
Class 1 SQL
Class 1 SQL
Select*from ap.invoices;
Select invoice_id,invoice_total
from ap.invoices;
/*This is a comment*/
use ap;
Select*from invoices;
Select*from vendors;
SELECT
FROM
invoices;
min(invoice_total) as minimum,
avg(invoice_total) as average
from invoices;
/* Where Clause*/
select*from invoices
where invoice_total>=1000;
/*Order By Clause*/
select*from invoices
where invoice_total>=1000
order by invoice_total
Limit 3;
Select*from vendors;
select count(distinct vendor_city) from vendors;
select*from vendors;
select*from vendors
select*from vendors
Class 2
use ap;
Select*from invoices
/*between*/
select*from invoices
Select*from vendors
where vendor_city="washington"
or vendor_city="New York"
or vendor_city="Dallas";
/*In statement*/
where vendor_city in
("washington","new York","dallas");
/*Group By*/
Select vendor_state, count(vendor_id) Numberofvendors
from vendors
Group by vendor_state
having Numberofvendors>2
from invoices
Group by vendor_id
/*subquery*/
/*joins*/
/*inner join*/
on invoices.vendor_id=vendors.vendor_id;
/* difference between primary and foreign key- primary key is the unique key in its own table and
foreign in other table*/
/*left join*/
on invoices.vendor_id=vendors.vendor_id;
/*right join*/
on invoices.vendor_id=vendors.vendor_id;
class 3
use ap;
Select*from invoices
/*between*/
select*from invoices
Select*from vendors
where vendor_city="washington"
or vendor_city="New York"
or vendor_city="Dallas";
/*In statement*/
where vendor_city in
("washington","new York","dallas");
/*Group By*/
Group by vendor_state
having Numberofvendors>2
from invoices
Group by vendor_id
/*subquery*/
/*joins*/
/*inner join*/
on invoices.vendor_id=vendors.vendor_id;
/* difference between primary and foreign key- primary key is the unique key in its own table and
foreign in other table*/
/*left join*/
select vendor_name, vendor_state, invoice_id, invoice_total
on invoices.vendor_id=vendors.vendor_id;
/*right join*/
on invoices.vendor_id=vendors.vendor_id;
/*inner join*/
use ap;
on i.vendor_id=v.vendor_id
/* difference between primary and foreign key- primary key is the unique key in its own table and
foreign in other table*/
/*right join*/
on i.vendor_id=v.vendor_id;
/*case statement*/
Select invoice_id,invoice_date,invoice_total,
end as Priority
from invoices
order by Priority;
/*generate a case statement column for write off less than 1000 yes otherwise no*/
Select invoice_id,invoice_date,invoice_total,
end as Priority,
else 'no'
end as writeoff
from invoices
order by Priority;
/*views- they are virtual tables with criteria, does not have physical existence, no physical table*/
select*from vendors1
where vendor_state="NY";
where vendor_state="NJ";
/*drop statement*/
Select invoice_id,invoice_date,invoice_total,
end as Priority,
else 'no'
end as writeoff
from invoices
order by Priority;
/*update command*/
update terms1
set terms_due_days=60
where terms_due_days=90;
/*creating table from scratch*/
Continue
use ap;
update terms1
set terms_due_days=60
where terms_due_days=90;
/*create database*/
use cat;
);
/*insert*/
values
("901","Sunidhi",99);
("902","Alia Bhatt",38);
values
("903","Katrina kaif",52);
select*from score;
Class 4 SQL
use ap;
Select*from invoices
/*between*/
select*from invoices
Select*from vendors
where vendor_city="washington"
or vendor_city="New York"
or vendor_city="Dallas";
/*In statement*/
where vendor_city in
("washington","new York","dallas");
/*Group By*/
from vendors
Group by vendor_state
having Numberofvendors>2
from invoices
Group by vendor_id
/*subquery*/
/*joins*/
/*inner join*/
on invoices.vendor_id=vendors.vendor_id;
/* difference between primary and foreign key- primary key is the unique key in its own table and
foreign in other table*/
/*left join*/
on invoices.vendor_id=vendors.vendor_id;
/*right join*/
on invoices.vendor_id=vendors.vendor_id;
/*inner join*/
use ap;
select vendor_name, vendor_state, invoice_id, invoice_total,v.vendor_id
on i.vendor_id=v.vendor_id
/* difference between primary and foreign key- primary key is the unique key in its own table and
foreign in other table*/
/*right join*/
on i.vendor_id=v.vendor_id;
/*case statement*/
Select invoice_id,invoice_date,invoice_total,
end as Priority
from invoices
order by Priority;
/*generate a case statement column for write off less than 1000 yes otherwise no*/
Select invoice_id,invoice_date,invoice_total,
end as Priority,
else 'no'
end as writeoff
from invoices
order by Priority;
/*views- they are virtual tables with criteria, does not have physical existence, no physical table*/
/*tables are physical and views are non-physical*/
select*from vendors1
where vendor_state="NY";
where vendor_state="NJ";
/*drop statement*/
Select invoice_id,invoice_date,invoice_total,
else 'no'
end as writeoff
from invoices
order by Priority;
/*update command*/
use ap;
update terms1
set terms_due_days=60
where terms_due_days=90;
/*create database*/
use cat;
(
cat_id int primary key,
);
/*insert*/
values
("901","Sunidhi",99);
values
("902","Alia Bhatt",38);
values
("903","Katrina kaif",52);
);
/*insert*/
values
("906","Sam",99);
values
("907","Ali",38);
insert into score1(cat_id, name, cat_score)
values
("908","Ram",52);
select*from score;
/*Union Query*/
use cat;
select*from score;
select*from score1;
Union
/*union all duplicate values cities will be allowed, whereas union takes only distinct values*/
where name="Sam";
/*Drop will delete all the table with all records whereas delete will drop certain records based on
conditions*/
Select*from consolidated;
Select*from consolidated;
/*Stored procedures*/
use ap;
Call GetPriorityInvoices();
Call GetVendorsbyState("NY");
Call GetVendorsbyState("NJ");
Call GetVendorsbyState("CA");
Call GetVendorsbyState("OH");
Call GetVendorStateCount("NJ",@TotalVendorsinState);
Select @TotalVendorsinState;
Call GetVendorStateCount("CA",@TotalVendorsinState);
Select @TotalVendorsinState;
use cat;
select*from randomcatfile;
Select @catscore;