Post #232
2.14K

SQL Mindmap
DA Showing posts older than #235 · Back to latest




INTERSECT and EXCEPT. Let’s take a closer look at how they work.INTERSECT OperatorINTERSECT operator is used to find rows that are present in both queries. It works like the intersection of sets in mathematics, returning only those records that exist in both datasets.SELECT column1, column2
FROM table1
INTERSECT
SELECT column1, column2
FROM table2;
table1 and table2.INTERSECT operator automatically removes duplicate rows from the result.EXCEPT OperatorEXCEPT operator is used to find rows that are present in the first query but not in the second. This is similar to the difference between sets, returning only those records that exist in the first dataset but are missing from the second.SELECT column1, column2
FROM table1
EXCEPT
SELECT column1, column2
FROM table2;
table1 but not in table2.EXCEPT operator also removes duplicate rows from the result.INTERSECT, the columns must have compatible data types.UNION, INTERSECT, and EXCEPT?UNION combines all rows from both queries, excluding duplicates.INTERSECT returns only the rows present in both queries.EXCEPT returns rows from the first query that are not found in the second.INTERSECT to identify customers who have made purchases both online and in physical stores.EXCEPT to find products that are sold in one store but not in another.INTERSECT and EXCEPT, you can simplify data analysis and work more flexibly with sets, making it easier to solve tasks related to finding intersections and differences between datasets. SELECT query.CREATE VIEW view_name AS
SELECT columns
FROM table_name
WHERE condition;
CREATE VIEW HighSalaryEmployees AS
SELECT name, salary
FROM employees
WHERE salary > 100000;
SELECT * FROM HighSalaryEmployees;
CREATE TABLE numeric_example (
id INT,
amount DECIMAL(10, 2)
);
CREATE TABLE string_example (
name VARCHAR(100),
description TEXT
);
CREATE TABLE datetime_example (
created_at DATETIME,
year_of_joining YEAR
);
INSERT INTO students (id, name, age)
VALUES (1, 'John Doe', 20);
SELECT * FROM students;
UPDATE students
SET age = 21
WHERE id = 1;
DELETE FROM students
WHERE id = 1;