0% found this document useful (0 votes)
22 views3 pages

AAAASSKAL

The document outlines SQL commands to create three tables: Customers, Orders, and Products, along with their respective fields and relationships. It includes sample data insertion for each table and various SQL queries demonstrating different types of joins (INNER, LEFT, RIGHT, and CROSS) as well as UNION operations. The queries aim to retrieve customer and order information, as well as relationships between customers based on their city.

Uploaded by

ferdjanbote497
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)
22 views3 pages

AAAASSKAL

The document outlines SQL commands to create three tables: Customers, Orders, and Products, along with their respective fields and relationships. It includes sample data insertion for each table and various SQL queries demonstrating different types of joins (INNER, LEFT, RIGHT, and CROSS) as well as UNION operations. The queries aim to retrieve customer and order information, as well as relationships between customers based on their city.

Uploaded by

ferdjanbote497
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/ 3

CREATE TABLE Customers (

customer_id INT PRIMARY KEY,

customer_name VARCHAR(50),

city VARCHAR(50)

);

CREATE TABLE Orders (

order_id INT PRIMARY KEY,

customer_id INT,

amount DECIMAL(10, 2),

FOREIGN KEY (customer_id) REFERENCES Customers(customer_id)

);

CREATE TABLE Products (

product_id INT PRIMARY KEY,

product_name VARCHAR(50),

price DECIMAL(10, 2)

);

INSERT INTO Customers VALUES

(1, 'Alice', 'New York'),

(2, 'Bob', 'Los Angeles'),

(3, 'Charlie', 'Chicago');

INSERT INTO Orders VALUES

(101, 1, 250.50),

(102, 2, 450.00),

(103, 3, 300.00),

(104, NULL, 500.00);

INSERT INTO Products VALUES

(201, 'Laptop', 1200.00),

(202, 'Phone', 800.00),

(203, 'Tablet', 500.00);


SELECT c.customer_id, c.customer_name, o.order_id, o.amount

FROM Customers c

INNER JOIN Orders o

ON c.customer_id = o.customer_id;

SELECT c.customer_id, c.customer_name, o.order_id, o.amount

FROM Customers c

LEFT JOIN Orders o

ON c.customer_id = o.customer_id;

SELECT c.customer_id, c.customer_name, o.order_id, o.amount

FROM Customers c

RIGHT JOIN Orders o

ON c.customer_id = o.customer_id;

SELECT c.customer_id, c.customer_name, o.order_id, o.amount

FROM Customers c

LEFT JOIN Orders o

ON c.customer_id = o.customer_id

UNION

SELECT c.customer_id, c.customer_name, o.order_id, o.amount

FROM Customers c

RIGHT JOIN Orders o

ON c.customer_id = o.customer_id;

SELECT c1.customer_id, c1.customer_name, c2.customer_id AS other_customer_id, c2.customer_name AS other_customer_name

FROM Customers c1

JOIN Customers c2

ON c1.city = c2.city AND c1.customer_id != c2.customer_id;

SELECT c.customer_name, p.product_name

FROM Customers c

CROSS JOIN Products p;

SELECT customer_id FROM Customers

UNION

SELECT customer_id FROM Orders;


SELECT customer_id FROM Customers

UNION ALL

SELECT customer_id FROM Orders;

You might also like