MYSQL Queries Part1
MYSQL Queries Part1
I will present a couple of query files from my practice,most of the codes having detailed
explanations written in-line.This are related first to basic sql procedures which will be
useful for further applications in EXCEL,VBA and Python.
You will learn how to create tables and databases,alter (by inserting rows and
columns),sort,select,having specific clauses and then export them.
Query 1
Create database tutorial;
Create TABLE t2
(v1 text,
v2 enum('Monday','Tuesday','Wednesday','Thursday')
);
Insert into t2 VALUES('Hello,today is','Tuesday');
select * from t2;
Query 2:
Use tutorial;
Create table afgan(c1 char(100),c2 char(100));/* you must right click
database and refresh
in order to be able to see the tables */
insert into afgan values("Hamir","Aziz");
insert into afgan values("Hassan","Abeba");
insert into afgan values("One","two");
rename table afgan to persons;
#alter table--adding columns
select count(*)
as no_of_persons from persons;#displays the number of persons inside the
persons table
select * from persons;
select * from persons
where c2 like '%h';#no item is selected
select * from persons
where c1 like '%h';#no item is selected since no name ends with h;
# MySql is not case sensitive
select * from persons
where c1 like 'H%';#two names start with H
select * from persons
where c1 like 'h%';
#Remark: If the wildcard is posed after the letter,it means that the letter
must be at the
#beginning,otherwise it's at the hend
select * from persons;
insert into persons values("Hannah","Montana",100,50);#not working,you need
now 4 parameters
insert into persons values("Xanax","Medicine",200);#same here
select * from persons
where c1 like '%h%';#all names that contain letter h
select c1 from persons
where age.null;#not working null is not an object of age
QUERY 3
use tutorial;
select * from persons;# i want to view the table
alter table persons
add city char(10);
update persons
set city='New York'
where c1 in ('Theodor','Hannah');
#Remark:you don't need to re-enter the select* from persons;you just run the
2nd line
#it works as a refresh button when you want to see again the whole table
select pers1.first_name,pers1.city,pers1.age,pers2.salary
from pers1
inner join pers2
on pers1.first_name=pers2.first_name;
PART3
use people;
select pers1.first_name,pers1.city,pers2.salary
from pers1
inner join pers2
on pers1.first_name=pers2.first_name
order by pers1.first_name;
select * from pers1;
select * from pers2;
select pers1.first_name,pers1.age,pers2.city
from pers1
left join pers2
on pers1.first_name=pers2.first_name;