SQL Tutorial - How to remove data using CTEs
Another video brought to you by BeardedDev, bringing you tutorials on Business Intelligence, SQL Programming and Data Analysis.
You can now support me on patreon - https://www.patreon.com/beardeddev
In this SQL Tutorial we remove data from a table using a CTE. We go through examples of populating duplicate data in a table then identifying the row number then deleting from the CTE which in turn deletes from the underlying table. We also discuss that when using this method there can only be one underlying table in the CTE and show an error that will be shown when joins are involved.
If you would like to see more video tutorials on CTEs check out my playlist: https://www.youtube.com/watch?v=3QuYuU9OJfA&list=PLgR-BOYibnN1jq0eQtCztDmNN6Af5RY-G
Code to follow tutorial:
-- check if Customers table exists
IF OBJECT_ID(N'dbo.Customers', N'U') IS NOT NULL
DROP TABLE dbo.Customers
-- create table Customers
CREATE TABLE dbo.Customers
(
FirstName VARCHAR(50)
, LastName VARCHAR(50)
, DOB DATE
, CityId INT
)
-- check if City table exists
IF OBJECT_ID(N'dbo.City', N'U') IS NOT NULL
DROP TABLE dbo.City
-- create table City
CREATE TABLE dbo.City
(
CityId INT
, City VARCHAR(50)
)
-- populate City table
INSERT INTO dbo.City (CityId, City)
VALUES
(1, 'Birmingham')
, (2, 'London')
, (3, 'Manchester')
, (4, 'Newcastle')
-- simulate inserting duplicate data, each customer is inserted 3x
INSERT INTO dbo.Customers (FirstName, LastName, DOB, CityId)
VALUES
('Tony', 'Smith', '19840407', 1)
, ('Michelle', 'Carter', '19951122', 2)
, ('Sarah', 'Gulliver', '19790730', 3)
, ('Matthew', 'Wilkins', '19880808', 4)
, ('Tony', 'Smith', '19840407', 1)
, ('Michelle', 'Carter', '19951122', 2)
, ('Sarah', 'Gulliver', '19790730', 3)
, ('Matthew', 'Wilkins', '19880808', 4)
, ('Tony', 'Smith', '19840407', 1)
, ('Michelle', 'Carter', '19951122', 2)
, ('Sarah', 'Gulliver', '19790730', 3)
, ('Matthew', 'Wilkins', '19880808', 4)
WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY FirstName, LastName, DOB, CityId ORDER BY FirstName) AS R
, *
FROM dbo.Customers
)
DELETE FROM CTE
WHERE R (insert greater than or equal to here) 2
Видео SQL Tutorial - How to remove data using CTEs канала BeardedDev
You can now support me on patreon - https://www.patreon.com/beardeddev
In this SQL Tutorial we remove data from a table using a CTE. We go through examples of populating duplicate data in a table then identifying the row number then deleting from the CTE which in turn deletes from the underlying table. We also discuss that when using this method there can only be one underlying table in the CTE and show an error that will be shown when joins are involved.
If you would like to see more video tutorials on CTEs check out my playlist: https://www.youtube.com/watch?v=3QuYuU9OJfA&list=PLgR-BOYibnN1jq0eQtCztDmNN6Af5RY-G
Code to follow tutorial:
-- check if Customers table exists
IF OBJECT_ID(N'dbo.Customers', N'U') IS NOT NULL
DROP TABLE dbo.Customers
-- create table Customers
CREATE TABLE dbo.Customers
(
FirstName VARCHAR(50)
, LastName VARCHAR(50)
, DOB DATE
, CityId INT
)
-- check if City table exists
IF OBJECT_ID(N'dbo.City', N'U') IS NOT NULL
DROP TABLE dbo.City
-- create table City
CREATE TABLE dbo.City
(
CityId INT
, City VARCHAR(50)
)
-- populate City table
INSERT INTO dbo.City (CityId, City)
VALUES
(1, 'Birmingham')
, (2, 'London')
, (3, 'Manchester')
, (4, 'Newcastle')
-- simulate inserting duplicate data, each customer is inserted 3x
INSERT INTO dbo.Customers (FirstName, LastName, DOB, CityId)
VALUES
('Tony', 'Smith', '19840407', 1)
, ('Michelle', 'Carter', '19951122', 2)
, ('Sarah', 'Gulliver', '19790730', 3)
, ('Matthew', 'Wilkins', '19880808', 4)
, ('Tony', 'Smith', '19840407', 1)
, ('Michelle', 'Carter', '19951122', 2)
, ('Sarah', 'Gulliver', '19790730', 3)
, ('Matthew', 'Wilkins', '19880808', 4)
, ('Tony', 'Smith', '19840407', 1)
, ('Michelle', 'Carter', '19951122', 2)
, ('Sarah', 'Gulliver', '19790730', 3)
, ('Matthew', 'Wilkins', '19880808', 4)
WITH CTE
AS
(
SELECT
ROW_NUMBER() OVER(PARTITION BY FirstName, LastName, DOB, CityId ORDER BY FirstName) AS R
, *
FROM dbo.Customers
)
DELETE FROM CTE
WHERE R (insert greater than or equal to here) 2
Видео SQL Tutorial - How to remove data using CTEs канала BeardedDev
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
![Pivot in SQL Server | How to create Pivot table in SQL by simple Query example, Where to used PIVOT](https://i.ytimg.com/vi/lsbHII5r9pk/default.jpg)
![Dynamic SQL in Stored Procedure](https://i.ytimg.com/vi/Gn8W_nLPz9k/default.jpg)
![SQL vs MySQL | Difference between SQL and MySQL | Intellipaat](https://i.ytimg.com/vi/7TBc5VbCVLk/default.jpg)
![SQL Tutorial - Nesting CASE statements](https://i.ytimg.com/vi/yS-f8T-6wZM/default.jpg)
![SQL Tutorial - Difference between CTEs and Derived Tables](https://i.ytimg.com/vi/LNzvB-dqSUg/default.jpg)
![SQL Joins Tutorial for Beginners - Inner Join, Left Join, Right Join, Full Outer Join](https://i.ytimg.com/vi/2HVMiPPuPIM/default.jpg)
![SQL Tutorial - How to create Views with Schemabinding](https://i.ytimg.com/vi/GoFsAFDz8JI/default.jpg)
![What is CTE ( Common Table Expression) in SQL Server ? | SQL Server CTE](https://i.ytimg.com/vi/BfKzryMx33M/default.jpg)
![Running an SQL Injection Attack - Computerphile](https://i.ytimg.com/vi/ciNHn38EyRc/default.jpg)
![SQL Tutorial - Subqueries (Scalar Valued)](https://i.ytimg.com/vi/5c2r97tDAjY/default.jpg)
![What is Dimension and Fact in Data Warehouse](https://i.ytimg.com/vi/DV4IMjnL_IU/default.jpg)
![SQL Tutorial - Logical Query Processing](https://i.ytimg.com/vi/sBRfBU5jh18/default.jpg)
![SQL Interview Questions - FizzBuzz Challenge](https://i.ytimg.com/vi/LnIY12v3EAc/default.jpg)
![How Water Towers Work](https://i.ytimg.com/vi/yZwfcMSDBHs/default.jpg)
![Part 2 SQL query to get organization hierarchy](https://i.ytimg.com/vi/Kd3HTph0Mds/default.jpg)
![SQL Server Programming Part 11 - Common Table Expressions (CTEs)](https://i.ytimg.com/vi/U0wXjUi2v_U/default.jpg)
![How to create Derived Tables in SQL Server](https://i.ytimg.com/vi/nxozAGkKFrg/default.jpg)
![Mechanical Vs. Electrical Engineering: How to Pick the Right Major](https://i.ytimg.com/vi/-XpXlc2Wux4/default.jpg)
![SQL Variable Declaration - How to Declare a Variable in SQL Server - Quick Tips Ep39](https://i.ytimg.com/vi/u4R46wlwAwo/default.jpg)
![What are Dirty Reads in SQL Server?](https://i.ytimg.com/vi/y06ExCznT_Y/default.jpg)