SQL-JOIN

 

The join clause combines columns of one table to that of another to create a single table. Join matches up a column with one table to a column in another table. A join query does not alter either table, but temporarily combines data from each table to be viewed as a single table. There are three types of join statements, inner, left, and right.

We will be using our employees table from previous examples, and a new table to track sales of each employee called invoices.

Our invoices table is set up with 3 fields, EmployeeID, Sale, and Price. If we were a business owner we now have a means to track what was sold, and by whom and we can bring this information together using an inner join clause.

Inner Join: An inner join returns all rows that result in a match such as the example above.

SQL Code:

SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
INNER JOIN invoices
ON employees.id = invoices.EmployeeID

SQL Table:

LastnameFirstnameSalePrice
JohnsonDavidHOT DOG1.99
HivelyJessicaLG SFT DRK1.49
DavisJulieCD SLD3.99
DavisJulieCD SLD3.99

We haven't changed or updated any information in either of our tables but we were able to fashion together a new table using a conditional that matches one table column to another.

SQL - Left Join

A Left join returns all rows of the left of the conditional even if there is no right column to match.

SQL Code:

SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
LEFT JOIN invoices
ON employees.id = invoices.EmployeeID

SQL Table:

LastnameFirstnameSalePrice
JohnsonDavidHOT DOG1.99
HivelyJessicaLG SFT DRK1.49
HicksFreddy  
HarrisJoel  
DavisJulieCD SLD3.99
DavisJulieCD SLD3.99

This would be a great way to track sales per person per day if the invoice table had a date field as well.

SQL - Right Join

A right join will display rows on the right side of the conditional that may or may not have a match.

SQL Code:

SELECT employees.Lastname, employees.Firstname, invoices.Sale, invoices.Price
FROM employees
RIGHT JOIN invoices
ON employees.id = invoices.EmployeeID

SQL Table:

LastnameFirstnameSalePrice
DavisJulieCD SLD3.99
DavisJulieCD SLD3.99
JohnsonDavidHOT DOG1.99
  HOT DOG1.99
HivelyJessicaLG SFT DRK1.49
  LG SFT DRK1.49

This would happen generally if perhaps nobody recieved credit for the sale or the sale was credited to the store by default.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值