Documente Academic
Documente Profesional
Documente Cultură
SQL commands
Class will start with refreshing the previous class with QA…. (30)
REGEXP COMMANDS
Query over column “last_name” of table “customer”
select *
from customer
where last_name regexp ‘b’ --b anywhere in the last name
select *
from customer
where last_name regexp ‘b|c’ --b or c anywhere in the last name
select *
from customer
where last_name regexp ‘[gim]e’ --ge, ie, or me anywhere in the last name
select *
from customer
where last_name regexp ‘e[gim]’ --eg, ei, or em anywhere in the last name
select *
1
from customer
where last_name regexp ‘[a-f]e’ --ae, be, ce, de, or fe anywhere in the last name
--similarly when last name starts with b will have the below command with REGEXP
select *
from customer
where last_name regexp ‘^b’
select *
from customer
where last_name regexp ‘^b|c|d’ --Either last name starts with b or contains c or d
--similarly when last name ends with b will have the following command with REGEXP
select *
from customer
where last_name regexp ‘b$’
select *
from customer
where last_name regexp ‘b$|c|d’ --last name ends with b or contains c or d
INNER JOIN
Inner join is the joining of 2 tables based on common column of both table to get a result table of
intended orientation/command. The result table will return the data only if the join condition is met.
Following are example of inner join.
Self joining
Is to join a table with the same with intended re-orientation.
For example from a table of all staff with name, we want a new table of staff with manager.
Alias the table name (let us use db sakila) during join command
select *
from city c
join address a
on c.city_id = a.city_id
Joining two table from different databases (suppose we are using db “sakila” and use table
“address” from db “mytutorial”)
select *
from city c
join <db>address a
on c.city_id = a.city_id
OUTER JOIN
Inner join will return rows where there's a matching row in both tables.
But outer join will return all the rows (from the left table in case of left join, from the right table
in case of right join) even there is no matching rows in the tables. In case full – all rows both
from left and right table returns.
3
Below example for inner and outer join
select
c.customer_id,
c.first_name,
c.last_name,
a.actor_id,
a.first_name,
a.last_name
from customer c left join actor a -- for inner join remove “left”, for right join replace”left”
on c.last_name = a.last_name; with “right” or “full” for full join.
SQL Code:
SELECT * FROM table_A
ON table_A.A=table_B.A;