added select and joins examples

This commit is contained in:
Sven Vogel 2023-12-01 16:31:58 +01:00
parent 7b08ad389b
commit 4e73beb3f2
2 changed files with 73 additions and 0 deletions

View File

@ -0,0 +1,37 @@
USE tutorial;
/* Cartesian product (every row combined with every other row) and filter out rows */
SELECT
em.id AS 'Employee ID',
em.surname AS 'Surname',
em.forename AS 'forename',
at.id AS 'Title ID',
at.name AS 'Title'
FROM
Employee AS em,
AcademicTitle AS at
WHERE
em.surname LIKE 'Ma%'
AND at.id = em.academicTitleId;
/* only select rows which have a matching (true) condition */
SELECT *
FROM Employee AS em
INNER JOIN AcademicTitle AS at
ON em.academicTitleId = at.id
WHERE
em.surname LIKE 'Ma%';
SELECT
em.surname,
em.roomId AS 'Employee Room',
room.id AS 'Room ID',
room.name AS 'Room name'
FROM Employee as em
INNER JOIN Room as room ON em.roomId = room.id
WHERE room.name LIKE 'a%';
SELECT
count(*) AS Anzahl
FROM Employee em
LEFT JOIN PostalLocation post ON post.id = em.postalLocationId;

36
tasks/joins.sql Normal file
View File

@ -0,0 +1,36 @@
USE tutorial;
SELECT positionId, forename, surname FROM Employee;
/* select only one of duplicates */
SELECT DISTINCT streetName FROM Employee;
/* add further conditions with where */
SELECT surname, EUR_GrossSalary FROM Employee
WHERE academicTitleId = 1
AND (EUR_GrossSalary BETWEEN 40000 AND 50000)
AND EUR_GrossSalary != 46000;
/* filter out non existing entries */
SELECT * FROM PostalLocation
WHERE cityDistrictName NOT LIKE ''
OR cityDistrictName IS NOT NULL;
/* check if something is a member of a list */
SELECT * FROM Employee WHERE academicTitleId IN (0, 1);
/* in with a sub query */
SELECT * FROM Employee WHERE academicTitleId IN (
SELECT academicTitleId FROM AcademicTitle WHERE academicTitleId > 1
);
/* check string for pattern */
/* %: zero or more characters */
/* _: one character */
SELECT surname FROM Employee WHERE surname LIKE 'A%';
/* where with regex */
SELECT surname FROM Employee WHERE REGEXP_LIKE(surname, '^[aB].*');
/* selects employees with have academic title of 1 or (exclusive) surname starts with B */
SELECT * FROM Employee WHERE academicTitleId = 1 XOR surname LIKE 'B';