SQL herhaling Flashcards

1
Q

SELECT … FROM … WHERE: Retrieve specific columns from a table based on a condition. Write a query to select the naam (name) column from the medewerker (employee) table where the functie (job title) is ‘TRAINER’.

A

SELECT naam
FROM medewerker
WHERE functie = ‘TRAINER’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

JOIN: Combine rows from two or more tables based on a related column. Write a query to retrieve the naam (name) from the medewerker table and join it with the inschrijving (enrollment) table on the corresponding id_medewerker and id_deelnemer columns.

A

SELECT medewerker.naam
FROM medewerker
INNER JOIN inschrijving ON medewerker.id_medewerker = inschrijving.id_deelnemer;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

ORDER BY: Sort the result set by one or more columns. Write a query to select maandsalaris (monthly salary) from medewerker and sort the results in descending order.

A

SELECT maandsalaris
FROM medewerker
ORDER BY maandsalaris DESC;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

GROUP BY: Group rows that have the same values in specified columns into summary rows. Write a query to select id_afdeling (department ID) and count the number of employees in each department from the medewerker table, grouping by id_afdeling.

A

SELECT id_afdeling, COUNT(*)
FROM medewerker
GROUP BY id_afdeling;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

WHERE … LIKE: Filter results based on a pattern. Write a query to select the omschrijving (description) from the cursus table where the omschrijving starts with ‘Java’.

A

SELECT omschrijving
FROM cursus
WHERE omschrijving LIKE ‘Java%’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Aggregate Functions (MIN, MAX, AVG, COUNT, SUM): Perform calculations on a set of values to return a single value. Write a query to find the maximum maandsalaris (monthly salary) from the medewerker table.

A

SELECT MAX(maandsalaris)
FROM medewerker;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

LIMIT: Restrict the number of rows returned by a query. Write a query to select the naam from the medewerker table, ordered by maandsalaris, and limit the result to the top 1 row.

A

SELECT naam
FROM medewerker
ORDER BY maandsalaris DESC
LIMIT 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

DISTINCT: Retrieve only unique values. Write a query to select all the unique functie (job title) from the medewerker table.

A

SELECT DISTINCT functie
FROM medewerker;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

LEFT JOIN (LEFT OUTER JOIN): Retrieve all rows from the left table and the matching rows from the right table. If there is no match in the right table, the result is NULL on the right side. Write a query to select omschrijving from cursus and id_deelnemer from inschrijving showing all courses even if there are no enrollments.

A

SELECT omschrijving, id_deelnemer
FROM cursus
LEFT JOIN inschrijving ON cursus.code = inschrijving.id_cursus;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Subquery: A query nested inside another query. Write a query to select naam from medewerker where their id_medewerker is in the result of a subquery that selects id_deelnemer from inschrijving for id_cursus = ‘S02’

A

SELECT naam
FROM medewerker
WHERE id_medewerker IN (SELECT id_deelnemer FROM inschrijving WHERE id_cursus = ‘S02’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

String Concatenation: Combine two or more strings into one. Write a query to concatenate naam (last name) and initialen_voornaam (initials) from the medewerker table with a space in between, aliasing the column as volledige_naam. Use || operator.

A

SELECT naam || ‘ ‘ || initialen_voornaam AS volledige_naam
FROM medewerker;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Date Functions (EXTRACT, AGE): Extract parts of a date or calculate the difference between dates. Write a query to extract the year from the geboortedatum (birthdate) of employees and calculate the age of each employee, aliasing the column as leeftijd. Use EXTRACT function.

A

SELECT EXTRACT(YEAR FROM AGE(NOW(), geboortedatum)) AS leeftijd
FROM medewerker;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

HAVING: Filter groups based on a condition after grouping with GROUP BY. Write a query to select id_cursus and calculate the average evaluatie from inschrijving, grouping by id_cursus, and only including groups where the average evaluation is greater than 3.

A

SELECT id_cursus, AVG(evaluatie)
FROM inschrijving
GROUP BY id_cursus
HAVING AVG(evaluatie) > 3;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Casting (::): Convert a value from one data type to another. Write a query to select maandsalaris from medewerker and cast it to NUMERIC(10, 2) to format it with two decimal places.

A

SELECT maandsalaris::NUMERIC(10, 2)
FROM medewerker;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly