SQL Tutorial - EXISTS

  Рет қаралды 5,022

BeardedDev

BeardedDev

Күн бұрын

Another video brought to you by BeardedDev, bringing you tutorials on Data Engineering, Business Intelligence, T-SQL Programming and Data Analysis.
If you like the video you can support me on Patreon, / beardeddev
In this SQL tutorial we take a look at exists, how to use exists with subqueries, how to create a correlated subquery and the difference between exists and in/not in. Exists can be used when we want to return results from one set based on whether the data exists in another set. Exists is not a replacement for joins and in the tutorial I discuss why, there are times when working with not in and NULL exists in the data that the results we are expecting are not returned but when using exists the results are what we expect.
If you would like to follow along with this SQL tutorial then you can use the below SQL to create the necessary objects.
Please feel free to post any comments.
DROP TABLE IF EXISTS dbo.T1;
CREATE TABLE dbo.T1
(
col1 TINYINT NULL
);
DROP TABLE IF EXISTS dbo.T2;
CREATE TABLE dbo.T2
(
col1 TINYINT NULL
);
INSERT INTO dbo.T1 (col1) VALUES (1), (2), (3);
INSERT INTO dbo.T2 (col1) VALUES (1), (2), (NULL);
SELECT * FROM dbo.T1;
SELECT * FROM dbo.T2;
DROP TABLE IF EXISTS dbo.Employees;
CREATE TABLE dbo.Employees
(
EmpId INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Position VARCHAR(50) NOT NULL
);
DROP TABLE IF EXISTS dbo.Customers;
CREATE TABLE dbo.Customers
(
CustId INT NOT NULL IDENTITY(1, 1) PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL
);
INSERT INTO dbo.Employees (FirstName, LastName, Position)
VALUES
('Deidre', 'Walsh', 'Sales Assistant'),
('Matthew', 'Arlington', 'Sales Assistant'),
('Michelle', 'Montgomery', 'Sales Assistant'),
('Lee', 'Chen', 'Sales Assistant');
INSERT INTO dbo.Customers (FirstName, LastName)
VALUES
('Deidre', 'Walsh'),
('Raphael', 'Jones'),
('Lee', 'Chen');

Пікірлер: 18
SQL Tutorial - How to use NTILE in SQL Server
15:28
BeardedDev
Рет қаралды 4,9 М.
T-SQL Tutorial - PIVOTing Made Easy
21:28
BeardedDev
Рет қаралды 10 М.
Seja Gentil com os Pequenos Animais 😿
00:20
Los Wagners
Рет қаралды 35 МЛН
MY HEIGHT vs MrBEAST CREW 🙈📏
00:22
Celine Dept
Рет қаралды 71 МЛН
啊?就这么水灵灵的穿上了?
00:18
一航1
Рет қаралды 68 МЛН
SQL Tutorial - Difference between CTEs and Derived Tables
28:53
BeardedDev
Рет қаралды 2,8 М.
EXIST Function in SQL
8:04
Edredo for Learners
Рет қаралды 122 М.
SQL Subquery using EXISTS and IN - SQL Tutorial #31
10:13
Data with Baraa
Рет қаралды 1,9 М.
T-SQL Tutorial - PIVOT without using PIVOT
15:43
BeardedDev
Рет қаралды 5 М.
Complex SQL Breakdown (CTEs, Subquery, and More)
22:14
Database Star
Рет қаралды 3,7 М.
Solving one of PostgreSQL's biggest weaknesses.
17:12
Dreams of Code
Рет қаралды 204 М.
SQL Tutorial - PIVOT with Text
16:36
BeardedDev
Рет қаралды 10 М.
Оператор EXISTS (T-SQL)
10:11
ITSpace
Рет қаралды 3,9 М.
Seja Gentil com os Pequenos Animais 😿
00:20
Los Wagners
Рет қаралды 35 МЛН