• Like
Unions and joins in mysql
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Published

Unions and joins in mysql

Unions and joins in mysql

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
248
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  • 2. Union and joins in mysqlShanu k k shanukk89@gmail.com www.facebook.com/shanunn i twitter.com/shanu in.linkedin.com/in/shanu 9656153432
  • 3. SQL UNION Operator: • The UNION operator is used to combine the result-set of two or more SELECT statements. • SELECT statement within the UNION must have the same number of columns • The columns must also have similar data type
  • 4. SQL UNION Syntax: SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;
  • 5. Cus_id Name Contact Name Address City PostalCod e Country 1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany 2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico 3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
  • 6. Suppliers" table: SupplierID SupplierName ContactNa me Address City PostalCod e Country 1 Exotic Liquid Charlotte Cooper 49 Gilbert St. Landona EC1 4SD UK 2 New Orleans Shelley Burke .O. Box 78934 New Orleans 70117 USA
  • 7. SELECT City FROM Customers UNION SELECT City FROM Suppliers ORDER BY City;
  • 8. CI TY Berlin Mexico D.F Mexico D.F Landona Neworleans
  • 9. • SQL JOIN: An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
  • 10. • INNER JOIN: Returns all rows when there is at least one match in BOTH tables • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table • FULL JOIN: Return all rows when there is a match in ONE of the tables
  • 11. SQL INNER JOIN Syntax SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name; • or: SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name=table2.column_name; • INNER JOIN is the same as JOIN.
  • 12. PK_DEPT_ID DEPT_NAME 1 ECE 2 CSE 3 IT
  • 13. Pk_class_id Class_name Fk_dept_id 1 111 1 2 112 1 3 234 1 4 222 2
  • 14. select dept_name ,class_name from tbl_dept inner join tbl_classes on tbl_classes.fk_dept_id=tbl_dept.pk_dept id;
  • 15. RIGHT JOIN The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.
  • 16. SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;
  • 17. FULL OUTER JOIN The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2).
  • 18. SQL FULL OUTER JOIN Syntax SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;
  • 19. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com