Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views14 pages

SQL

Uploaded by

Senyo Kaledzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views14 pages

SQL

Uploaded by

Senyo Kaledzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

https://drive.google.com/file/d/1n_YZ39I3XHUTY78lLU1EAwcDjPknbsDC/view?

usp=drivesdk

https://drive.google.com/file/d/11FIQGl5HMiHINkFUhzQ4DdmIVzpGrIBB/view?usp=drivesdk

https://drive.google.com/file/d/11FIQGl5HMiHINkFUhzQ4DdmIVzpGrIBB/view?usp=drivesdk

https://drive.google.com/file/d/1lpNs0zVrfTpQ4xQ-HIvaF1xW1kiiqBqr/view?usp=drivesdk

https://drive.google.com/file/d/1lpNs0zVrfTpQ4xQ-HIvaF1xW1kiiqBqr/view?usp=drivesdk

https://docs.google.com/document/d/11rZqrtB5quqqb1AzT1dN_Obe2HqogBGfBqxBGCJgesk/edit?
usp=drivesdk

https://docs.google.com/document/d/11rZqrtB5quqqb1AzT1dN_Obe2HqogBGfBqxBGCJgesk/edit?
usp=drivesdk

===============================

-- Lesson 21: INTERSECT and EXCEPT

-- ===============================

-- Create a new database for this lesson

CREATE DATABASE SQLSetOperatorsDB;

GO

-- Use the new database

USE SQLSetOperatorsDB;
GO

-- Create two tables with overlapping and unique data

CREATE TABLE Customers_US (

CustomerID INT PRIMARY KEY,

CustomerName VARCHAR(100),

Country VARCHAR(50)

);

CREATE TABLE Customers_UK (

CustomerID INT PRIMARY KEY,

CustomerName VARCHAR(100),

Country VARCHAR(50)

);

-- Insert sample data for US Customers

INSERT INTO Customers_US (CustomerID, CustomerName, Country)

VALUES

(1, 'Alice', 'USA'),


(2, 'Bob', 'USA'),

(3, 'Charlie', 'USA'),

(4, 'Diana', 'USA'),

(5, 'Evelyn', 'USA');

-- Insert sample data for UK Customers

INSERT INTO Customers_UK (CustomerID, CustomerName, Country)

VALUES

(3, 'Charlie', 'UK'),

(4, 'Diana', 'UK'),

(5, 'Evelyn', 'UK'),

(6, 'Frank', 'UK'),

(7, 'Grace', 'UK');

https://docs.google.com/document/d/1Owzf_XRVNEjtYU7CO418JCEymcJHcaiuhg3NUIRpeXk/edit?
usp=drivesdk

https://docs.google.com/document/d/1Owzf_XRVNEjtYU7CO418JCEymcJHcaiuhg3NUIRpeXk/edit?
usp=drivesdk

===============================
-- Database for 10-Day Subquery Mastery

-- ===============================

-- Create database

CREATE DATABASE SQLSubqueryDB;

GO

-- Use the new database

USE SQLSubqueryDB;

GO

-- ===============================

-- Table: Customers

-- ===============================

CREATE TABLE Customers (

CustomerID INT IDENTITY(1,1) PRIMARY KEY,

CustomerName NVARCHAR(100) NOT NULL,

Country NVARCHAR(50),

RegistrationDate DATE
);

GO

INSERT INTO Customers (CustomerName, Country, RegistrationDate) VALUES

('Alice', 'USA', '2023-01-15'),

('Bob', 'USA', '2023-02-20'),

('Charlie', 'UK', '2023-03-05'),

('Diana', 'UK', '2023-04-12'),

('Evelyn', 'Canada', '2023-05-30'),

('Frank', 'Canada', '2023-06-15'),

('Grace', 'USA', '2023-07-01'),

('Henry', 'UK', '2023-07-20');

GO

-- ===============================

-- Table: Orders

-- ===============================

CREATE TABLE Orders (


OrderID INT IDENTITY(1,1) PRIMARY KEY,

CustomerID INT NOT NULL,

OrderDate DATE NOT NULL,

OrderAmount DECIMAL(10,2) CHECK (OrderAmount >= 0),

Status NVARCHAR(20) CHECK (Status IN ('Pending','Completed','Cancelled')),

FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)

);

GO

INSERT INTO Orders (CustomerID, OrderDate, OrderAmount, Status) VALUES

(1, '2023-07-01', 150.50, 'Completed'),

(2, '2023-07-03', 200.00, 'Pending'),

(3, '2023-07-05', 75.00, 'Completed'),

(4, '2023-07-07', 120.00, 'Cancelled'),

(5, '2023-07-10', 250.00, 'Completed'),

(6, '2023-07-12', 300.00, 'Pending'),

(7, '2023-07-15', 180.00, 'Completed'),


(8, '2023-07-18', 90.00, 'Completed'),

(1, '2023-07-20', 130.00, 'Completed'),

(3, '2023-07-22', 60.00, 'Pending');

GO

-- ===============================

-- Table: Products

-- ===============================

CREATE TABLE Products (

ProductID INT IDENTITY(1,1) PRIMARY KEY,

ProductName NVARCHAR(100) NOT NULL,

Category NVARCHAR(50),

Price DECIMAL(10,2) CHECK (Price >= 0)

);

GO

INSERT INTO Products (ProductName, Category, Price) VALUES

('Laptop', 'Electronics', 1200.00),


('Phone', 'Electronics', 800.00),

('Desk', 'Furniture', 300.00),

('Chair', 'Furniture', 150.00),

('Notebook', 'Stationery', 5.00),

('Pen', 'Stationery', 2.00);

GO

-- ===============================

-- Table: OrderDetails

-- ===============================

CREATE TABLE OrderDetails (

OrderDetailID INT IDENTITY(1,1) PRIMARY KEY,

OrderID INT NOT NULL,

ProductID INT NOT NULL,

Quantity INT CHECK (Quantity > 0),

UnitPrice DECIMAL(10,2) CHECK (UnitPrice >= 0),


FOREIGN KEY (OrderID) REFERENCES Orders(OrderID),

FOREIGN KEY (ProductID) REFERENCES Products(ProductID)

);

GO

INSERT INTO OrderDetails (OrderID, ProductID, Quantity, UnitPrice) VALUES

(1, 1, 1, 1200.00),

(1, 5, 3, 5.00),

(2, 2, 2, 800.00),

(3, 6, 10, 2.00),

(4, 3, 1, 300.00),

(5, 4, 2, 150.00),

(6, 1, 1, 1200.00),

(7, 2, 1, 800.00),

(8, 5, 5, 5.00),

(9, 3, 1, 300.00),

(10, 6, 4, 2.00);
GO

-- ===============================

-- Table: Employees

-- ===============================

CREATE TABLE Employees (

EmployeeID INT IDENTITY(1,1) PRIMARY KEY,

EmployeeName NVARCHAR(100),

Department NVARCHAR(50),

HireDate DATE

);

GO

INSERT INTO Employees (EmployeeName, Department, HireDate) VALUES

('John', 'Sales', '2022-01-10'),

('Emma', 'Sales', '2022-02-15'),

('Liam', 'IT', '2022-03-20'),

('Olivia', 'IT', '2022-04-25'),


('Noah', 'HR', '2022-05-30'),

('Ava', 'HR', '2022-06-15');

GO

-- ===============================

-- Table: EmployeeOrders (many-to-many)

-- ===============================

CREATE TABLE EmployeeOrders (EmployeeID INT NOT NULL,

OrderID INT NOT NULL,

PRIMARY KEY (EmployeeID, OrderID),

FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID),

FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)

);

GO

INSERT INTO EmployeeOrders (EmployeeID, OrderID) VALUES

(1,1),(1,2),(2,3),(2,4),(3,5),(3,6),(4,7),(4,8),(5,9),(6,10);

GO
-- End of SQLSubqueryDB Script

https://docs.google.com/document/d/1DZUHB_-L3hTSHlFBBxRrI3jqGxsoNRCtQ4QuVcPvhAg/edit?
usp=drivesdk

https://docs.google.com/document/d/1DZUHB_-L3hTSHlFBBxRrI3jqGxsoNRCtQ4QuVcPvhAg/edit?
usp=drivesdk

Create a new database for Union lesson

CREATE DATABASE SQLUnionDB;

GO

-- Switch to the new database

USE SQLUnionDB;

GO

-- Create first table: Customers from USA

CREATE TABLE Customers_USA (

CustomerID INT PRIMARY KEY,

FirstName VARCHAR(50),

LastName VARCHAR(50),

City VARCHAR(50)
);

-- Insert sample data into Customers_USA

INSERT INTO Customers_USA (CustomerID, FirstName, LastName, City)

VALUES

(1, 'John', 'Smith', 'New York'),

(2, 'Alice', 'Brown', 'Chicago'),

(3, 'David', 'Wilson', 'Los Angeles'),

(4, 'Emma', 'Taylor', 'Miami');

-- Create second table: Customers from UK

CREATE TABLE Customers_UK (

CustomerID INT PRIMARY KEY,

FirstName VARCHAR(50),

LastName VARCHAR(50),

City VARCHAR(50)

);

-- Insert sample data into Customers_UK


INSERT INTO Customers_UK (CustomerID, FirstName, LastName, City)

VALUES

(5, 'Oliver', 'Jones', 'London'),

(6, 'Sophia', 'Davis', 'Manchester'),

(7, 'John', 'Smith', 'Newcastle'), -- Same name as in USA (for duplicates demo)

(8, 'Emily', 'Clark', 'Liverpool');

GO

You might also like