CREATE TABLE "customer" (
"id" INTEGER NOT NULL,
"firstName" TEXT NOT NULL,
"lastName" TEXT NOT NULL,
"birthDate" date NOT NULL,
"phone" TEXT NOT NULL,
"address" TEXT NOT NULL,
"city" TEXT NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
);
Let's insert some records so that we have duplicates in the record for the city and for the name.
SELECT 1 + 1;
SELECT * FROM customer
SELECT * FROM customer ORDER BY firstName DESC
SELECT * FROM customer ORDER BY firstName DESC LIMIT 1
SELECT firstName, lastName FROM customer ORDER BY firstName DESC
SELECT * FROM customer WHERE id = 1
SELECT * FROM customer WHERE firstName = "Stepan"
SELECT count() FROM customer WHERE firstName = "Stepan"
SELECT count() FROM customer WHERE firstName = "Stepan" OR firstName = "Petr"
SELECT * FROM customer WHERE id BETWEEN 1 AND 5
SELECT * FROM customer WHERE firstName LIKE "Step"
SELECT * FROM customer WHERE firstName LIKE "Step%"
SELECT * FROM customer WHERE firstName LIKE "%tep%"
SELECT * FROM customer WHERE firstName LIKE "%p%"
SELECT DISTINCT firstName FROM customer //two same names in records
UPDATE customer SET firstName = "Ludek" WHERE id = 1
DELETE FROM customer WHERE firstName = "Ludek"
CREATE TABLE "order" (
"id" INTEGER NOT NULL,
"customer_id" INTEGER NOT NULL,
"date" date NOT NULL,
"comment" TEXT,
FOREIGN KEY("customer_id") REFERENCES "customer"("id") ON UPDATE CASCADE,
PRIMARY KEY("id" AUTOINCREMENT)
);
INSERT INTO "order"
("customer_id", "date", "comment")
VALUES (1, '2024-05-12', 'Please ship as soon as possible!');
SELECT *
FROM customer AS c
INNER JOIN "order" AS o on c.id = o.customer_id;
SELECT *
FROM customer AS c
LEFT JOIN "order" AS o on c.id = o.customer_id;
VS
SELECT *
FROM "order" AS o
INNER JOIN customer AS c on c.id = o.customer_id;
SELECT *
FROM "order" AS o
LEFT JOIN customer AS c on c.id = o.customer_id;
Compare the joining of two tables with another type of joining.
CREATE TABLE "product" (
"id" INTEGER NOT NULL,
"name" TEXT NOT NULL,
"stock" INTEGER NOT NULL,
"price" decimal(4, 2) NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE "order_product" (
"id" INTEGER NOT NULL,
"order_id" INTEGER NOT NULL,
"product_id" INTEGER NOT NULL,
"quantity" INTEGER NOT NULL,
FOREIGN KEY("product_id") REFERENCES "product"("id") ON UPDATE CASCADE,
FOREIGN KEY("order_id") REFERENCES "order"("id") ON UPDATE CASCADE,
PRIMARY KEY("id" AUTOINCREMENT)
);
SELECT *
FROM "order" AS o
LEFT JOIN order_product AS op on o.id = op.order_id
LEFT JOIN product AS p on op.product_id = p.id
LEFT JOIN customer AS c on c.id = o.customer_id
If we have more customers than orders.
SELECT *
FROM "customer" AS c
LEFT JOIN "order" AS o on c.id = o.customer_id
LEFT JOIN order_product AS op on o.id = op.order_id
LEFT JOIN product AS p on op.product_id = p.id
From a different side
SELECT *
FROM "product" AS p
INNER JOIN order_product AS op on p.id = op.product_id
INNER JOIN "order" AS o on o.id = op.order_id
INNER JOIN customer AS c on o.customer_id = c.id
SELECT city, count(*) FROM customer
GROUP BY city
SELECT order_id, p.name, p.price, c.firstName, c.lastName
FROM "order" AS o
LEFT JOIN order_product AS op on o.id = op.order_id
LEFT JOIN product AS p on op.product_id = p.id
LEFT JOIN customer AS c on c.id = o.customer_id
SELECT order_id, c.firstName, c.lastName, sum(price)
FROM "order" AS o
LEFT JOIN order_product AS op on o.id = op.order_id
LEFT JOIN product AS p on op.product_id = p.id
LEFT JOIN customer AS c on c.id = o.customer_id
GROUP BY order_id