Sunteți pe pagina 1din 4

LESSON-10

SQL commands
 Class will start with refreshing the previous class with QA…. (30)

Today’s topics: Lesson plan

1. SQL Commands (45)


2. SQL Joins and commands (105)

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.

Joining multiple table


In case of joining table A, B, and C, the command will be …
2
select *
from A
join B
on A.matching row = B.matching row
join C
on B.matching row = C.matching row
Similarly many tables can be joined together as per intended result. In the place of * we can put the
column as per our requirement.

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

select c.city, a.address, postal_code


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.

Example: SQL FULL OUTER JOIN (let us try in cloudera)

SQL Code:
SELECT * FROM table_A

FULL OUTER JOIN table_B

ON table_A.A=table_B.A;

S-ar putea să vă placă și