SQL Tutorial - EXISTS
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, https://www.patreon.com/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');
Видео SQL Tutorial - EXISTS канала BeardedDev
If you like the video you can support me on Patreon, https://www.patreon.com/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');
Видео SQL Tutorial - EXISTS канала BeardedDev
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
![SQL Tutorial - Subqueries (Scalar Valued)](https://i.ytimg.com/vi/5c2r97tDAjY/default.jpg)
![We Learn SQL #11 | Subqueries in SQL with operators ANY and ALL](https://i.ytimg.com/vi/nD7JAdEQYAE/default.jpg)
![Running an SQL Injection Attack - Computerphile](https://i.ytimg.com/vi/ciNHn38EyRc/default.jpg)
![Let's Learn SQL! Lesson 82 : EXIST and NOT EXIST Subqueries](https://i.ytimg.com/vi/GnYyX40Oclg/default.jpg)
![THE BEST Multimeter tutorial (HD)](https://i.ytimg.com/vi/bF3OyQ3HwfU/default.jpg)
![What are the SQL Subquery and Exists Clause Statement Language Elements? (Part 6 of 8)](https://i.ytimg.com/vi/EfkvHDsLwi0/default.jpg)
![SQL Tutorial - Window Functions - Calculate Running Totals, Averages](https://i.ytimg.com/vi/6S7z2wabJxk/default.jpg)
![The Science of Awkwardness](https://i.ytimg.com/vi/o268qbb_0BM/default.jpg)
![SQL Server video :- Explain aggregate functions,group by clause and having keyword ?](https://i.ytimg.com/vi/K2mFsfhLckw/default.jpg)
![Learn SQL in 1 Hour - SQL Basics for Beginners](https://i.ytimg.com/vi/9Pzj7Aj25lw/default.jpg)
![SQL Tutorial - Joining on NULLs](https://i.ytimg.com/vi/5Yqe1_29GIs/default.jpg)
![Advanced Aggregate Functions in SQL (GROUP BY, HAVING vs. WHERE)](https://i.ytimg.com/vi/nNrgRVIzeHg/default.jpg)
![SOLVE 5 SQL QUERIES IN 5 MINUTES (PART 1) | MASTER IN SQL | SQL INTERVIEW QUESTIONS](https://i.ytimg.com/vi/OIRy7ZFSudA/default.jpg)
![SQL Tutorial - Working with Variables](https://i.ytimg.com/vi/6tRgeEKVXNk/default.jpg)
![SQL Tutorial - Window Functions - Ranking](https://i.ytimg.com/vi/xL7BtCT7ix4/default.jpg)
![SQL: Difference Between IN and EXISTS Operator](https://i.ytimg.com/vi/ZhNVrC6lvBs/default.jpg)
![SQL Tutorial - UNPIVOT](https://i.ytimg.com/vi/0ERIr8UAK1s/default.jpg)
![Hacking Websites with SQL Injection - Computerphile](https://i.ytimg.com/vi/_jKylhJtPmI/default.jpg)
![SQL Server DBA Tutorial 75-How to Create Backup Maintenance Plan in SQL Server](https://i.ytimg.com/vi/jb2gsNinIb0/default.jpg)
![EXIST Function in SQL](https://i.ytimg.com/vi/HM6Jc5YGubQ/default.jpg)