From 4e73beb3f26f780d15e91f759fc7baf0504ef288 Mon Sep 17 00:00:00 2001 From: servostar Date: Fri, 1 Dec 2023 16:31:58 +0100 Subject: [PATCH] added select and joins examples --- tasks/Wiederholung-01.12.2023.sql | 37 +++++++++++++++++++++++++++++++ tasks/joins.sql | 36 ++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tasks/Wiederholung-01.12.2023.sql create mode 100644 tasks/joins.sql diff --git a/tasks/Wiederholung-01.12.2023.sql b/tasks/Wiederholung-01.12.2023.sql new file mode 100644 index 0000000..b6958a3 --- /dev/null +++ b/tasks/Wiederholung-01.12.2023.sql @@ -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; \ No newline at end of file diff --git a/tasks/joins.sql b/tasks/joins.sql new file mode 100644 index 0000000..922f4d9 --- /dev/null +++ b/tasks/joins.sql @@ -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'; \ No newline at end of file