首页 > 数据库 >[928] SQL Tutorial

[928] SQL Tutorial

时间:2023-10-27 12:56:52浏览次数:53  
标签:rows Outer 928 Join SQL table matching Tutorial

ref: Structured Query Language (SQL)

ref: Inner Join vs Outer Join

ref: SQL Self Join

ref: SQL | Functions (Aggregate and Scalar Functions)

ref: SQL | NULL functions

ref: SQL | Numeric Functions

ref: SQL | String functions

ref: SQL | Advanced Functions


A generic query to retrieve from a relational database is:

SELECT [DISTINCT] Attribute_List FROM R1,R2….RM
[WHERE condition]
[GROUP BY (Attributes)[HAVING condition]]
[ORDER BY(Attributes)[DESC]];

Inner Join/Simple Join

In an INNER join, it allows retrieving data from two tables with the same ID.

An Inner Join returns only the matching rows between the two tables based on a specified condition. It combines data from two tables based on a common column between them, which is specified using the ON keyword in SQL. Only the rows that meet the join condition from both tables are returned. If a row in one table does not have a matching row in the other table, that row will not be included in the result set.

Inner Join

Inner Join

Syntax:

SELECT COLUMN1, COLUMN2 FROM

 [TABLE 1] INNER JOIN [TABLE 2] 

ON Condition;


How To Use Inner Join?

Inner Join is basically performed by just selecting the records having the common values or the matching values in both tables. In case of no common values, no data is shown in the output.

Syntax:

Select Table1.Col_Name, Table2.Col_Name....
From Table1
Inner Join Table2
on Table1.Common_Col = Table2.Common_Col;

If there are 3 Tables present in the database, then the Inner Join works as follows:

Select Table1.Col_Name, Table2.Col_Name, Table3.Col_Name....
From ((Table1
Inner Join Table2
on Table1.Common_Col = Table2.Common_Col)
Inner Join Table3
on Table1.Common_Col = Table3.Common_Col);

Outer Join

An Outer Join returns all the rows from one table and matching rows from the other table based on a specified condition. It combines data from two tables based on a common column between them, which is also specified using the ON keyword in SQL. In addition to the matching rows, it also includes rows from one table that do not have matching rows in the other table.

Outer Join is of three types:

  1. Left outer join 
  2. Right outer join 
  3. Full Join

1. Left Outer join 

Left Outer Join returns all rows of a table on the left side of the join. For the rows for which there is no matching row on the right side, the result contains NULL on the right side.

Left Outer Join returns all the rows from the left table and matching rows from the right table. If a row in the left table does not have a matching row in the right table, the result set will include NULL values for the columns in the right table.

Left Outer Join

Left Outer Join

Syntax:

SELECT  T1.C1, T2.C2

 FROM TABLE T1 

LEFT JOIN TABLE T2 

ON T1.C1= T2.C1;

 

2. Right Outer Join 

Right Outer Join is similar to Left Outer Join (Right replaces Left everywhere). Right Outer Join returns all the rows from the right table and matching rows from the left table. If a row in the right table does not have a matching row in the left table, the result set will include NULL values for the columns in the left table.

Right Outer Join

Right Outer Join

Syntax:

SELECT T1.C1, T2.C2

 FROM TABLE T1 

RIGHT JOIN TABLE T2 

ON T1.C1= T2.C1;

 

3. Full Outer Join 

Full Outer Join contains the results of both the Left and Right outer joins. It is also known as cross-join. It will provide a mixture of two tables. 

Full Outer Join returns all the rows from both tables, including matching and non-matching rows. If a row in one table does not have a matching row in the other table, the result set will include NULL values for the columns in the table that do not have a match.

Full Outer Join

Full Outer Join

Syntax:

SELECT * FROM T1 

CROSS-JOIN T2;


SQL Self Join

Joins in SQL, a self join is a regular join that is used to join a table with itself. It basically allows us to combine the rows from the same table based on some specific conditions. It is very useful and easy to work with, and it allows us to retrieve data or information which involves comparing records within the same table.

Syntax:

SELECT columns

FROM table AS alias1

JOIN table AS alias2 ON alias1.column = alias2.column;

 

标签:rows,Outer,928,Join,SQL,table,matching,Tutorial
From: https://www.cnblogs.com/alex-bn-lee/p/17792096.html

相关文章

  • openEuler安装postgresql
    yuminstall-ygccmakereadline-develzlib-devellibicu-develcd/usr/localtarzxvfpostgresql-12.16.tar.gzcdpostgresql-12.16./configure--prefix=/usr/local/postgresqlmake-j8&&makeinstalladduserpgsqlmkdir/usr/local/postgresql/datach......
  • SQL Server常用命令
     --重建索引dbccdbreindex('表名','',90) --清除数据库日志use[数据库]selectFILE_ID,name,size,*fromsys.database_files--查询数据库及日志名称alterdatabase[数据库名称]setrecoverysimplewithno_waitalterdatabase[数据库名称]setrecoverysimple--简......
  • mysql5.7启用ssl连接(windows版本)
    环境:OS:windows2012Mysql:5.7.29 1.安装mysql安装步骤省略,mysql5.7默认安装都已经安装好了ssl的,并默认启用了的.证书在数据目录下 D:\mysql57\dataD:\mysql57\data>dir驱动器D中的卷是新加卷卷的序列号是7603-6C5BD:\mysql57\data>dir*.pem驱动器D中的......
  • 本地docker搭 建开源MySQL审计平台 yearning
     简单介绍:Yearning是一个轻量级的Web端的MySQLSQL语句审核平台,提供查询审计,SQL审核,SQL回滚,自定义工作流等多种功能。Yearning的前端是基于Vue.js构建的,它还提供SQL语法高亮、自动补全和智能提示、可视化等。Yearning自身包含了一套通常适用的审核规范,基本上能满足日常需要,同......
  • Sybench性能测试MySQL/TIDB
    一、安装部署1.1源码/包:https://github.com/akopytov/sysbench1.2安装工具Sysbench1.0.20版本切换root用户执行方式一:-Debian/Ubuntu系统:curl-shttps://packagecloud.io/install/repositories/akopytov/sysbench/script.deb.sh|sudobashsudoapt-yinstallsys......
  • plsql中文乱码问题
    原文地址:https://blog.csdn.net/qq_39939541/article/details/131401316一、plsql版本  二、乱码现象  三、解决办法 1.查询数据库字符集selectuserenv('language')fromdual    2.查询本地字符集select*fromV$NLS_PARAMETERS  发现数据......
  • mysql统计 不含的月份补0
    构造当前日期之前10天的日期表 可以把DAY换成MONTH构造需要的月份  不需要固定天数的可以去掉limitSELECT@cdate:=date_add(@cdate,INTERVAL-1DAY)dateFROM(SELECT@cdate:=date_add(CURDATE(),INTERVAL1DAY)FROMAPPLYLIMIT10)a ......
  • sqli-master第一关
    sqli-master第一关 打开后发现让我们输入ID输入?id=1回车返回下图名字和密码 尝试后面添加'发现报错 orderby猜解字段,到4时发现报错,说明字段为3 unionselect显示可显示路段替换2为database()得出数据库名为securityunionselect1,group_concat(table_name),3......
  • 常见面试题-MySQL专栏(一)
    为什么mysql删了行记录,反而磁盘空间没有减少?答:在mysql中,当使用delete删除数据时,mysql会将删除的数据标记为已删除,但是并不去磁盘上真正进行删除,而是在需要使用这片存储空间时,再将其从磁盘上清理掉,这是MySQL使用延迟清理的方式。延迟清理的优点:如果mysql立即删除数据,会导......
  • 41 mysql 索引和慢查询优化
    MySQL之索引原理与慢查询优化 阅读目录一介绍二索引的原理三索引的数据结构四聚集索引与辅助索引五MySQL索引管理六测试索引七正确使用索引八联合索引与覆盖索引九查询优化神器-explain十慢查询优化的基本步骤十一慢日志管理一介绍为何要......