Skip to content

Introduction to SQL Joins

SQL is an abbreviation for a Structured Query Language and pronounced either see-kwell or as separate letters. The spread of dynamic websites on the World Wide Web today is largely due to the possibility for their content to be handled through databases. Database management is a complicated process, which has been considerably rationalized by the SQL programming language. As its full name (Structured Query Language) implies, SQL is responsible for querying and editing information stored in a certain database management system.

 

SQL CREATE TABLE

CREATE TABLE statement is used for creating tables in a database. Tables are organized in rows and columns. Where columns are the attributes and rows are known as records.

CREATE TABLE table_name(
column1 datatype,
   column2 datatype,
   column3 datatype,
   .....
   column datatype,
   PRIMARY KEY( one or more columns )
);

CREATE TABLE Example

 
CREATE TABLE Employees (
    EmpID INT NOT NULL,
    FirstName VARCHAR(50) NOT NULL,
    LastName VARCHAR(50) NOT NULL,
    Age INT NOT NULL,
    EmailID VARCHAR(50) NOT NULL,
    PhoneNo INT NOT NULL,
    City VARCHAR(25) NOT NULL,
    PRIMARY KEY (EmpID)
);

The above statement would create a table named “Employees” in the database having the primary key as “EmpID”.

Note: Since EmpID is a primary key it cannot have duplicate values as this column is used for identifying the unique record (row) from the table.

 

INSERT INTO Statement

The INSERT INTO statement of SQL is used to insert a new row in a table. There are two ways of using the recordset INSERT INTO statement for inserting rows:

Only values: The first method is to specify only the value of data to be inserted without the column names.

Syntax

INSERT INTO table_name VALUES (value1, value2, value3,…);

Example

INSERT INTO Employees VALUES ('102','Kazim','Mohd','24','kazim@abc.com','1190702985','Delhi'),
('156','Brijesh','Sharma','30','brijesh@abc.com','1187665892','Agra'),
('148','Sakshi','Aswal','25','sakshi@abc.com','1196583701','Bengaluru');

Output

Column names and values both: In the second method we will specify both the columns which we want to fill and their corresponding values.

 

Syntax

INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2, value3,..);

Example

INSERT INTO 
    Employees ( 
        EmpID, FirstName, LastName, Age, EmailID, PhoneNo, City
        )
        
VALUES
    ('121','Alok','Awasthi','27','alok@abc.com','1149484126','Kanpur'),
    ('265','Bishwajeet Kumar','Singh','25','bishwajeet@abc.com','1186573520','Noida'), ('164','Nazim','Mohd','22','nazim@abc.com','1109648295','Mumbai'),
            (‘312’,’Raman’,’Kumar’,’32’,’raman@abc.com’,’1186047397’,’Kolkata’);

Output

SQL JOINS

A SQL join is an SQL clause that is used to join records from two or more tables in a database. The records from the tables are combined by a common attribute that the tables share, resulting in a single record set. This record set can be inserted into a temporary table to be stored and used. You can also specify additional criteria in the join to filter out records (besides sharing the common attribute).

 

Types of JOINS

  • SQL INNER JOIN (JOIN)
  • SQL LEFT OUTER JOIN (LEFT JOIN)
  • SQL RIGHT OUTER JOIN (RIGHT JOIN)
  • SQL FULL OUTER JOIN (FULL JOIN)

Let us look into each one of them. For your better understanding of this concept, I will be considering the following three tables to show you how to perform the Join operations on such tables.

Employees Table

Projects Table

 

Clients Table

INNER JOIN

Inner join is the most commonly used. It only combines records from the two tables if they both match the join condition (share a common attribute). This joins work best when referential integrity is enforced in the database, especially on primary and foreign keys.

Syntax

SELECT Table1.Column1,Table1.Column2,Table2.Column1,Table2.Column2,....
FROM Table1
INNER JOIN Table2
ON Table1.MatchingColumnName = Table2.M

Example

SELECT Employees.EmpID, Employees.FirstName,Projects.ProjectID,Projects.ProjectName,Projects.ProjectStartDate
FROM Employees
INNER JOIN Projects ON Employees.EmpID=Projects.EmpID;

Output

LEFT JOIN

The left join means that the join will favor the left listed (first listed) table. Favoring the table means that all results from that table will be shown in the result, whether or not they match the joined table on the condition. If they do not match any rows in the joined table, they will be attached to null columns.

Syntax

SELECT Table1.Column1,Table1.Column2,Table2.Column1,Table2.Column2....
FROM Table1
LEFT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

Example

SELECT Employees.EmpID, Employees.FirstName, Projects.ProjectID, Projects.ProjectName
FROM Employees
LEFT JOIN Projects
ON Employees.EmpID = Projects.EmpID ;

Output

RIGHT JOIN

A right join returns all rows from the RIGHT-hand table specified in the ON condition and only those rows from the other table where the joined fields are equal (join condition is met).

SELECT Table1.Column1,Table1.Column2,Table2.Column1,Table2.Column2....
FROM Table1
RIGHT JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

Example

SELECT Projects.ProjectID,Projects.ProjectName,Clients.ClientID,Clients.ClientName
FROM Projects
RIGHT JOIN Clients
ON Projects.ClientID = Clients.ClientID;

Output

FULL JOIN

A full join means that both tables are favored, so all rows from each table will be listed in the result regardless of whether they match any rows in the other table. In practice, these joins are fairly rare.

Syntax

SELECT Table1.Column1,Table1.Column2,Table2.Column1,Table2.Column2,....
FROM Table1
FULL JOIN Table2
ON Table1.MatchingColumnName = Table2.MatchingColumnName;

Example

SELECT Projects.ProjectID,Projects.ProjectName,Clients.ClientID,Clients.ClientName
FROM Projects
FULL JOIN Clients
ON Projects.ClientID = Clients.ClientID;

Output

Conclusion

SQL joins are commonplace in the database world as normalized databases contain many tables. Without them, there would not be an efficient way to retrieve the data that you are looking for. Utilizing the four types will help you write better queries, and retrieve the proper data efficiently.

Introduction to SQL Joins
You may Also Like
Scroll to top

代做工资流水公司漳州企业对公流水查询沈阳开流水宁波对公银行流水打印重庆打印背调流水上饶入职银行流水制作岳阳办背调工资流水贵阳背调流水查询杭州查贷款工资流水南阳转账流水报价福州开日常消费流水温州流水单多少钱深圳代做企业对公流水绵阳流水代做北京车贷工资流水太原签证工资流水费用沧州查询工资证明黄冈银行流水PS代开沈阳制作企业流水打印肇庆代办自存流水合肥离职证明代做铜陵查询收入证明舟山薪资流水单代办天津代做企业贷流水绵阳签证工资流水公司杭州房贷银行流水 查询廊坊转账流水代办信阳银行流水修改价格临沂打银行流水PS舟山背调工资流水查询遵义查询企业对公流水香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声卫健委通报少年有偿捐血浆16次猝死汪小菲曝离婚始末何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言男子被猫抓伤后确诊“猫抓病”周杰伦一审败诉网易中国拥有亿元资产的家庭达13.3万户315晚会后胖东来又人满为患了高校汽车撞人致3死16伤 司机系学生张家界的山上“长”满了韩国人?张立群任西安交通大学校长手机成瘾是影响睡眠质量重要因素网友洛杉矶偶遇贾玲“重生之我在北大当嫡校长”单亲妈妈陷入热恋 14岁儿子报警倪萍分享减重40斤方法杨倩无缘巴黎奥运考生莫言也上北大硕士复试名单了许家印被限制高消费奥巴马现身唐宁街 黑色着装引猜测专访95后高颜值猪保姆男孩8年未见母亲被告知被遗忘七年后宇文玥被薅头发捞上岸郑州一火锅店爆改成麻辣烫店西双版纳热带植物园回应蜉蝣大爆发沉迷短剧的人就像掉进了杀猪盘当地回应沈阳致3死车祸车主疑毒驾开除党籍5年后 原水城县长再被查凯特王妃现身!外出购物视频曝光初中生遭15人围殴自卫刺伤3人判无罪事业单位女子向同事水杯投不明物质男子被流浪猫绊倒 投喂者赔24万外国人感慨凌晨的中国很安全路边卖淀粉肠阿姨主动出示声明书胖东来员工每周单休无小长假王树国卸任西安交大校长 师生送别小米汽车超级工厂正式揭幕黑马情侣提车了妈妈回应孩子在校撞护栏坠楼校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变老人退休金被冒领16年 金额超20万西藏招商引资投资者子女可当地高考特朗普无法缴纳4.54亿美元罚金浙江一高校内汽车冲撞行人 多人受伤

代做工资流水公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化