TP 4
TP 4
Clause Significance
SELECT Retrieves data from one or more tables
Clause Significance
FROM Specifies the table(s) to retrieve data from
EXISTS/NOT EXISTS Checks if rows exist that match a condition
WHERE Filters rows that meet a specified condition
ANY/ALL Compares values to any or all values in a set
ORDER BY Sorts the result set by one or more columns
INTERSECT Combine rows returned by multiple SELECT statements
LIMIT Limits the number of rows returned
EXCEPT Get rows in first query except those in second query
OFFSET Skips a number of rows before returning the result set
UNION Combines the result set of multiple SELECT statements
CASE Allows conditional logic and branching in a query
LIKE Searches for a pattern in a column
IN Matches values against a set of values
BETWEEN Selects values within a range
IS NULL Tests for null values
DISTINCT Return unique values
AVG() Calculates the average of a column
COUNT() Counts rows matching the query criteria
MIN(), MAX() Finds the minimum and maximum value in a column
SUM() Calculates the total sum of values in a column
ASC/DESC Specify sort order as ascending or descending
Here are 5 example SQL queries using various clauses with explanations:
1. SELECT * FROM books ORDER BY ISNB ASC NULL;
Retrieves all columns from the books table
1
École Supérieure en Sciences et Technologies de l’Informatique et du Numérique
2nd Cycle Department 1 st year Speciality Class 2023 – 2024 Databases
Practical Assignments 04 :
Database Interrogation Language
Orders the results by the ISBN column in ascending order
Puts null values for ISBN at the end of the result set
2. SELECT DISTINCT authorName FROM authors;
Retrieves only the distinct name from the author’s table
Removes any duplicate city values using DISTINCT
3. SELECT * FROM authors WHERE age BETWEEN 35 AND 55;
Gets all columns for authors
WHERE limits rows to only authors with an age between 35 and 55
4. SELECT title, genre FROM books ORDER BY publicationDate DESC LIMIT 10;
Retrieves title and genre columns
Orders by publication date in descending order (high to low)
LIMIT returns only the first 10 rows
5. SELECT COUNT(ISNB) FROM books WHERE title LIKE ‘_a%y’
Counts the number of books_id values from the books table
WHERE filters title second letter is a ‘A’ and last is ‘Y’
Returns a count of all books ISNB with that filter
Application
Use the database created in the previous part and write the appropriate queries to implement
the following functions.
2
École Supérieure en Sciences et Technologies de l’Informatique et du Numérique
2nd Cycle Department 1 st year Speciality Class 2023 – 2024 Databases
Practical Assignments 04 :
Database Interrogation Language
7. Select the top 5 most recently published books
8. Select authors aged between 40 and 60 years old
9. Select publishers located in 'USA, UK'
10. Select all books with the genre 'Fiction' published between 2020 and 2022.
11. Select book titles published between 2018 and 2022 by 'Penguin'
12. Select different headquarters Locations of the publishers
13. Select member names and addresses located in 'New York'
14. Select the youngest and the oldest author
15. Select authors whose name starts with 'J' and is not null
16. How many books were loaned
17. Select publishers founded after 2000
18. Select members who have an email address from 'gmail.com'
19. Select books published between 2015 and 2020 by 'Harper'
20. Select the number of publishers located in 'London'
21. Select the top 10 most recently published books
22. Select the average years of book publication
23. Select authors with a gmail or yahoo email address
24. Select authors who live in the (USA or UK)
25. Retrieve the book titles published in both 2020 and 2021.
26. Categorize members into states based on whether their address is in California, New York or
Other.
27. Combine the list of book titles and publisher headquarters locations into one result.
28. Classify member phone numbers as either starting with 555 or Other.
29. Retrieve old book titles published before 2000 and publisher websites for publishers founded
before 1950.
30. Get book titles published after any book from 2020.
31. Retrieve book titles published after all books from 2015.
32. Find book titles that match publisher headquarters locations.
33. List author names that are not also member names.
3
École Supérieure en Sciences et Technologies de l’Informatique et du Numérique
2nd Cycle Department 1 st year Speciality Class 2023 – 2024 Databases
Practical Assignments 04 :
Database Interrogation Language
34. Retrieve books 11-15 ordered by most recent publication date.
35. Find authors above the average age in their country.
36. Get books published by publishers located in New York.