The JOINS command in SQL

Typically, the JOINS statement in SQL is used to connect information in different tables; the JOINS condition is part of an SQL query to retrieve rows from two or more tables.

The JOINS condition in SQL is used in SELECT, UPDATE, and DELETE statements with the WHERE clause in SQL .

The JOINS command in SQL

The syntax for the JOINS command in SQL to link two tables is:

SELECT col1, col2, col3.

FROM table_name1, table_name2

WHERE table_name1.col2 = table_name2.col1;

If the JOINS condition in SQL is omitted or is invalid, it will return the Cartesian product of the set of records from two or more connected tables. The Cartesian product returns the row number equivalent to the product of all rows in the connected tables.

For example, if the first table has 20 rows and the second table has 10 rows, the result would be 20 * 10, which is equivalent to 200 rows. This query takes a long time to execute.

Example of the JOINS command in SQL

Use SQL JOINS to connect the two tables below:

Product database table :

The order_items database table :

SQL JOINS are divided into two types: Equi join and Non-Equi join .

SQL Equi join

The condition of the JOINS statement in SQL uses the equals sign (=) as the comparison operator. Equi joins are divided into two types: SQL Outer join and SQL Inner join .

For example, you can retrieve information about customers who purchased products and the quantity purchased.

SQL Non-equivalent join

This condition uses other comparison operators, such as >, <, >=, <>

1. Learn about SQL Equi Join

An equi-join is classified into two types:

- SQL Inner Join

- SQL Outer Join

1.1. SQL Inner Join

All rows returned by the SQL query satisfy the specified SQL connection condition.

Example of SQL Inner Join:

If you want to display product information for each order, the query would look like this. Since you are retrieving data from two tables, you need to identify the common column between the two tables, which is product_id.

The SQL query has the following form:

SELECT order_id, product_name, unit_price, supplier_name, total_units

FROM product, order_items

WHERE order_items.product_id = product.product_id;

The columns must be referenced by the table name in the Join condition, because product_id is a column present in both tables and needs to be identified in the same way. This is to avoid using columns in the SELECT statement in SQL.

The number of the Join condition is (n-1) if there are more than 2 tables joined in a query, where n is the number of related tables. The rule must be true to avoid Cartesian product.

Alternatively, you can also use aliases to reference column names; the query will look like this:

SELECT o.order_id, p.product_name, p.unit_price, p.supplier_name, o.total_units

FROM product p, order_items o

WHERE o.product_id = p.product_id;

1.2. SQL Outer Join

The Join condition returns all rows from both tables that meet the join condition, along with rows that do not meet the condition from one of the tables. The SQL Outer Join operator in Oracle is (+) and is only used on one side of the Join condition.

Different RDBMS implementation syntaxes vary. Very few of these syntaxes represent join conditions such as "sql left outer join", "sql right outer join".

If you want to display all data in the product table along with the data in the order items table, with null values ​​displayed for orders if a product is not in stock, the SQL Outer Join query would look like this:

SELECT p.product_id, p.product_name, o.order_id, o.total_units

FROM order_items o, product p

WHERE o.product_id (+) = p.product_id;

Note: If the (+) operator is used on the left side of the Join condition, it is equivalent to a left outer join. If used on the right side of the Join condition, it is equivalent to a right outer join.

SQL Self Join

SQL Self Join is a type of JOIN in SQL, used to join two separate tables, especially when the table has a FOREIGN KEY referencing a PRIMARY KEY. It is essential to ensure that the JOIN statement specifies aliases for both copies of the table.

The following is an example of a SQL Self Join query:

SELECT a.sales_person_id, a.name, a.manager_id, b.sales_person_id, b.name

FROM sales_person a, sales_person b

WHERE a.manager_id = b.sales_person_id;

2. SQL Non-Equivalent Join

Non-Equivalent Join is a conditional SQL JOIN statement that uses all comparison operators except the equals operator (=). Examples include > =, <=, > <, >.

Example of SQL Non-Equivalent Join:

If you want to find the names of students who are not enrolled in economics, the SQL query would look like this (using the student information table that TipsMake has introduced in previous articles):

SELECT first_name, last_name, subject

FROM student_details

WHERE subject != 'Economics'

The output will look like this:


Above is detailed information and examples of the JOINS command in SQL. Basically, the JOINS command in SQL is used to connect information in different tables. Additionally, you can also learn more about the UPDATE command in SQL here. If you have any questions or need clarification, please leave a comment in the section below the article.

Other SQL articles
  • How to deploy SQL Server via PowerShell DSC

    hôm nay, tipsmake sẽ hướng dẫn các bạn cách triển khai sql server thông qua powershell dsc giúp bạn có thể sử dụng tính năng powershell mới quan lý cơ sở hạ tầng trong cơ sở, trên dịch vụ đám mây hiệu quả, dễ dàng.
  • How to create, validate, and modify the Identity column in Microsoft SQL Server

    microsoft sql server được tích hợp sẵn cột identity, được sử dụng để tạo ra các giá trị quan trọng. nó có thể tham chiếu nhưng một trường autonumber trong microsoft access hoặc một dãy trong oracle. bài viết dưới đây tipsmake sẽ hướng dẫn bạn cách tạo, kiểm tra và thay đổi cột indentity trên microsoft sql server.
  • How to log in to SQL Server if the SA account is disabled.

    trong trường hợp nếu tài khoản sa bị vô hiệu hóa và bạn không thể đăng nhập sql server. bài viết này tipsmake sẽ hướng dẫn bạn cách đăng nhập sql server nếu tài khoản sa bị vô hiệu hóa.
  • Instructions on how to install Laravel, supporting web programming.

    với những ai đang học lập trình không thể nào không biết đến được laravel, vậy làm thế nào để cài đặt laravel trên máy tính của bạn, chạy trơn chu các ứng dụng web cũng như giúp bạn học tập và thực hành tốt.
  • How to install SQL Server 2019 on Windows

    phiên bản microsoft sql server 2019 mới nhất mang đến rất nhiều tính năng mới hấp dẫn đáp ứng được người dùng hiện nay, nhất là giúp tổ chức đánh giá, kiểm tra dữ liệu và tích hợp nhiều phần mềm khác nhau. Để cài đặt microsoft sql server 2019, các bạn làm theo hướng dẫn trong bài viết sau đây.
  • The AND, OR, and NOT operators in SQL

    toán tử and và or được sử dụng để kết hợp nhiều điều kiện nhằm thu hẹp dữ liệu trong câu lệnh sql. hai toán tử này được gọi là toán tử liên hợp trong sql. and và or cho phép tạo nhiều so sánh với các toán tử khác trong cùng một lệnh sql.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup