0% found this document useful (0 votes)
64 views4 pages

MYSQL Queries Part1

Here are described 3 first query files working with usual queries : INSERT,ALTER(Table&Database),SELECT,JOINS,WHERE clauses

Uploaded by

theodor_munteanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views4 pages

MYSQL Queries Part1

Here are described 3 first query files working with usual queries : INSERT,ALTER(Table&Database),SELECT,JOINS,WHERE clauses

Uploaded by

theodor_munteanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MYSQL Introduction

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

Alter table persons


add salary double;

insert into persons values("Theodor","Constantin",200);


insert into persons values("Mark","Twain",300);
#you must add all parameters now not only the first name and name but also
the salary

Select * from persons;# to display the table


delete from persons
where salary=NULL;# no row affected
#Remark: when you restart a database you have to re-run the Use database code
before any
#other action desired is to be completed
select * from persons;

Alter table persons


add age int; # we are adding another column

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

select c1 from persons


where (c1 like '%h' OR c1 like'h%');#we have 3 names,Hamir,Hassan and Hannah

insert into persons values("Konah","Mrzat",200,33);


select c1 from persons
where (c1 like '%h' Or c1 like 'h%');#the last name doesn't start with H but
ends with H
#hence it is selected

select * from persons


where (c1 like 'T%' or c1 like 'H%'); /* selects all the persons whose first
name starts with
T or H*/
select * from persons;

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

Create table alternative as


Select distinct c1,c2,salary,city from persons;/* We create another table
based upon the persons
table but only with 4 columns */
/* Exercise 1:
Create two tables pers1 and pers2 inside another database,called people.Each
table will have
two columns:first_name and city.Add items from pers2 to pers1 and then make
all possible operations
on them */

create database people;


use people;
create table pers1(first_name char(20),city char(30));
create table pers2(first_name char(20),city char(30));

insert into pers1(first_name,city)


values('Theodor','London'),('Brigitte','Nancy'),('Helen','Birmingham');
#this is how insert multiple values
#THe general format is insert into
table_name(col_name1,col_name2,...,col_namek)
#values('colvalue1','colvalue2',...),('colvalue1','colvalue2',...) as many
times you want
#... means you have k values.
select * from pers1;
insert into pers2(first_name,city)
values('Jim Jin','Seoul'),('Han Ji','Shanghai'),('Lu Li','Shenzen');
select * from pers2;

insert into pers1(first_name,city)


select first_name,city from pers2;/* we introduce with no conditions
whatsoever all elements
from pers 2 into pers 1*/

Alter table pers2


add salary double;

Alter table pers1


add age double;

Select * from pers1,pers2;

# We will create an inner join between the 2 tables


#we select all champs from pers1 that have the same first_name as in pers_2

select pers1.first_name,pers1.city,pers1.age,pers2.salary
from pers1
inner join pers2
on pers1.first_name=pers2.first_name;

# we select one column less


select pers1.first_name,pers1.city,pers2.salary
from pers1
inner join pers2
on pers1.first_name=pers2.first_name;
select pers1.first_name,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 first_name from pers1;

#left join vs right join


#first,we update pers2
update pers2
set pers2.salary=2500
where first_name in ('Jim Jin','Han Ji');
update pers2
set pers2.salary=3000
where first_name='Lu Li';

select pers1.first_name,pers1.age,pers2.city
from pers1
left join pers2
on pers1.first_name=pers2.first_name;

You might also like