Sql inner join keyword (with quiz)

1,507 views
1,235 views

Published on

Programmer : Meryll Darlene Tiangco
Quiz Maker : Richmond Jove Medina
Documenter : Amanda Rosa Blanca Arevalo
Presenter : Chuck Loven Ballcer
Mark Anthony Yamaguchi
Emmaunel Ponce

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,507
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
41
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Sql inner join keyword (with quiz)

  1. 1. What is SQL ? SQL stands for Structured Query Language. SQL is a special-purpose programming language designed for managing data held in a relational database management system (RDBMS). SQL is used to communicate with a database and lets you access and manipulate databases.
  2. 2. SQL statements are used to perform tasks such as update data on a database, or retrieve data from a database. Some common relational database management systems that use SQL are: Oracle, Sybase, Micro soft SQL Server, and Access.
  3. 3. What can SQL do ? SQL can execute queries against a database SQL can retrieve data from a database SQL can insert records in a database SQL can update records in a database SQL can delete records from a database SQL can create new databases SQL can create new tables in a database
  4. 4. What is a Relational Database System ? A relational database system contains one or more objects called tables. The data or information for the database are stored in these tables. Tables are uniquely identified by their names and are comprised of columns and rows.
  5. 5. Columns contain the column name, data type, and any other attributes for the column. Rows contain the records or data for the columns. Columns City Low Manila 101 92 Makati 88 69 Quezon City Rows High 77 60 Sample Table “Weather Temperature”
  6. 6. One of the most beneficial features of SQL & relational database systems Makes relational database systems "relational". Used to combine rows from two or more tables, based on a common field between them. Allows you to link data from two or more tables together into a single query result-from one single SELECT statement.
  7. 7. What would happen if you worked with one table only, and didn't have the ability to use “JOIN". Let's say you have a one-table database that is used to keep track of all of your customers and what they purchase from your store: CID_No Surname First Address Date Item Price
  8. 8. Every time a new row is inserted into the table, all columns will be updated, thus resulting in unnecessary redundant data. For example, every time Juan Tamad purchases something, the following rows will be inserted into the table: CID_No. Surname First Address Date Item Price 10908 Tamad Juan 14 Sipag St., Manila 01/07/14 1K Rice 40.00 10908 Tamad Juan 14 Sipag St., Manila 01/08/14 Purefoods Corned Beef 85.00 10908 Tamad Juan 14 Sipag St., Manila 01/09/14 555 Sardines 25.00
  9. 9. To eliminate unnecessary redundant data, our database should have two tables : 1. One for keeping track of your customer 2. And the other to keep track of what they purchase : TABLE 1 Cust_Info Table TABLE 2 Purchases Table CID_No Surname First CID_No Date Item Address Price Now, whenever a purchase is made from a repeating customer, the 2nd table, "Purchases" only needs to be updated , eliminating useless redundant data.
  10. 10. Notice that both tables have a common “CID_No" column. TABLE 1 Cust_Info Table CID_No TABLE 2 Purchases Table CID _No Surname Date First Item Address Price This column, which contains the unique customer id number, will be used to JOIN the two tables. Table 1 Cust_Info Table 2 Purchases CID_No
  11. 11. The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables. SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name; Notice that each of the columns are always preceded with the table name and a period. This is a good practice so that you wont confuse which columns go with what tables. It is also required if the name column names are the same between the two tables.
  12. 12. To JOIN the Cust_Info Table and the Purchases Table using the CID_No column that is found in both tables, we have : column_name(s) SELECT cust_info.first, cust_info.surname, purchases.item FROM cust_info INNER JOIN purchases table1 table 2 ON cust_info.cid_no = purchases.cid_no; table1.column_name table2.column_name
  13. 13. If we execute these statements, using the data in the tables below : TABLE 1 Cust_Info Table CID_No Surname First Address Clavio Arnold 7 Tahimik St, Quezon City 10908 Tamad Juan 14 Sipag St. Malate, Manila 10909 TABLE 2 Purchases Table 10907 Santos Charo Laya Ave., Muntinlupa City CID_No 10908 10909 10907 10908 10907 10908 Date 01/07/14 01/07/14 01/07/14 01/08/14 01/08/14 01/09/14 Item 1K rice Papaya Soap 1K Ampalaya Purefoods Corned Beef 5K rice 555 Sardines Price 40.00 140.00 60.00 85.00 350.00 25.00
  14. 14. We will have something like this : First Surname Item Arnold Clavio 1K Ampalaya Arnold Clavio 5K rice Juan Tamad 1K rice Juan Tamad Purefoods Corned Beef Juan Tamad 555 Sardines Charo Santos Papaya Soap
  15. 15. TABLE ORDER : STATEMENT 1 STATEMENT 2 SELECT * SELECT* FROM Table1 FROM Table2 INNER JOIN Table2 INNER JOIN Table1 ON Table1.column_name ON Table2.column_name *Table2.column_name *Table1.column_name
  16. 16. SPECIFIED COLUMNS : SELECT specified_column/s FROM Table1 INNER JOIN Table2 ON Table1.column_name = Table2.column_name ALTERNATIVE : SELECT * FROM Table1,Table2 WHERE Table1.column_name = Table2.column_name
  17. 17. INNER JOIN requires the existence of a match between the tables based on specified column/s and only matching records are selected. The OUTER JOIN keyword does not require each record in the two joined tables to have a matching record. The joined table retains each record— even if no other matching record exists.
  18. 18. Depending on which table’s rows are retained, OUTER JOIN is subdivided into : LEFT JOIN - Returns all rows from the left table for the first tables. Even if there are no matches in the right.
  19. 19. RIGHT JOIN - Return all the rows from the right table ( or the second table) even if there are no matches in the left table
  20. 20. FULL JOIN - Both tables are secondary (or optional) such that if rows are being matched in tabled A and table B then all rows from table A are displayed even if there is no matching row in table B and vice versa
  21. 21. Look at the tables below and note the common column names between them : EmployeeID EmpName 01-054 01-055 Jerry Mendoza Dane Sevilla 01-056 TABLE 1 Employee Table Mylene Antonio LogID 1 2 LogDate 11-09-11 11-10-11 3 TABLE 2 LogRecordsTable EmployeeID 01-054 01-056 01-054 11-11-11 TimeRecID TABLE 3 TimeRecordsTable 1 2 LogID TIn TOut 1 09:00 18:03 2 07:30 16:55 3 3 09:02 18:34
  22. 22. SELECT * FROM Employee AS e LEFTJOIN LogRecords AS L ON e.EmployeeID = L.EmployeeID LEFT JOIN TimeRecords AS t ON t.LogID = L.LogID Employee LogRecords TimeRecords Table 1 Table 2 Table 3 e L t
  23. 23. Employee EmpName ID Jerry 01-054 Mendoza Jerry 01-054 Mendoza Dane 01-055 Sevilla Mylene 01-056 Antonio LogID EmployeeID LogDate TimeRecID LogID TIn TOut 1 01-054 11-09-11 1 1 09:00 18:03 3 01-054 11-11-11 3 3 9:02 18:34 Null Null Null Null Null 2 01-056 11-10-11 2 2 7:30 16:55 NULL is returned where no there are no matches in the RIGHT table Null Null
  24. 24. SELECT e.EmpName, L.LogDate, t.Tin, t.Tout FROM Employee AS e LEFTJOIN LogRecords AS L ON e.EmployeeID = L.EmployeeID LEFT JOIN TimeRecords AS t ON t.LogID = L.LogID EmpName LogDate TIn TOut Jerry Mendoza 11-09-11 09:00 18:03 Jerry Mendoza 11-11-11 9:02 18:34 Dane Sevilla Null Null Null Mylene Antonio 11-10-11 7:30 16:55
  25. 25. SELECT e.EmpName, L.LogDate, t.Tin, t.Tout FROM Employee AS e LEFTJOIN LogRecords AS L ON e.EmployeeID = L.EmployeeID LEFT JOIN TimeRecords AS t ON t.LogID = L.LogID WHERE L.LogDate IS NOT NULL EmpName LogDate TIn TOut Jerry Mendoza 11-09-11 09:00 18:03 Jerry Mendoza 11-11-11 9:02 18:34 Mylene Antonio 11-10-11 7:30 16:55
  26. 26. SELECT e.EmpName, L.LogDate, t.Tin, t.Tout FROM TimeRecords AS t RIGHT JOIN LogRecords AS L ON t.LogID = L.LogID RIGHT JOIN Employee AS e ON e.EmployeeID = L.EmployeeID EmpName LogDate TIn TOut Jerry Mendoza 11-09-11 09:00 18:03 Jerry Mendoza 11-11-11 9:02 18:34 Dane Sevilla Null Null Null Mylene Antonio 11-10-11 7:30 16:55
  27. 27. In FULL JOIN, both tables are secondary (or optional). In this case, if we are matching rows in table A and B, then we display : all rows from table A even if there is no matching row in table B TABLE A TABLE B all rows from table B even if there is no matching row in table A
  28. 28. Look at the tables below and note the common column names between them : TABLE 1 MovieList MovieID MovieTitle 1 Saw 4 2 Bad Teacher 3 The Notebook 4 Shrek 5 Schindler’s List 6 The Exorcist TABLE 2 GenresTable GenreCode HR CY RC No code available HS HR GenreCode GenreDesc HR Horror RC Romance HS History CY Comedy SF Sci-Fi
  29. 29. SELECT MovieTitle, GenreDesc FROM MovieList AS m FULLJOIN Genres AS g ON m.GenreCode = g.GenreCode MovieTitle Saw 4 Bad Teacher The Notebook Shrek Schindler’s List The Exorcist NULL GenreDesc Horror Comedy Romance NULL History Horror Sci-Fi
  30. 30. JOIN’s are used to combine & get the data from different tables. INNER JOIN returns rows when there is a match in both tables. LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. RIGHT JOIN returns all rows from the right table, even if there are no matches in the left table. FULL JOIN returns rows when there is a match in one of the tables.
  31. 31. NULL values are returned in the output when no records are returned by an OUTER JOIN statement JOIN queries can be used with the conjunction of SELECT We can also use WHERE clauses along with JOIN’s
  32. 32. Identify the type of JOIN shown in the following diagrams :
  33. 33. Identify the following : 1. This is used to communicate with a database and lets you access and manipulate databases. 2. This is used to combine and get data from different tables. 3. This returns rows when there is a match in one of the tables.
  34. 34. 4. This returns rows when there is a match in both tables. 5. This returns all rows from the left table, even if there are no matches in the right table. 6. This is where one or more objects called tables are contained. 7. This allows you to link data from two or more tables together into a single query result from one single SELECT statement
  35. 35. 8. This returns all rows from the right table, even if there are no matches in the left table. 9. This is value returned in the output when no records are returned by an OUTER JOIN statement. 10. This conjunction can be used with JOIN queries.

×