Select Queries
This chapter demonstrates how SQL queries are translated into SQLAlchemy ORM queries. Each concept follows the same structure:- Task
- SQL (SQLite)
- SQLAlchemy Solution
- Expected Output
Preparation
Student Model
File:models.py
Sample Data
File:main.py
Basic SELECT
Task
Retrieve all students.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
WHERE Clause
Task
Retrieve all students whose age is 21 or above.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Comparison Operators
Greater Than (>)
Task
Retrieve all students whose marks are greater than 90.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Less Than (<)
Task
Retrieve all students whose age is less than 20.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Greater Than or Equal To (>=)
Task
Retrieve all students whose marks are 85 or above.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Less Than or Equal To (<=)
Task
Retrieve all students whose age is 20 or below.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Equal To (==)
Task
Retrieve all students from Hyderabad.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Not Equal To (!=)
Task
Retrieve all students who are not enrolled in the Python course.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
AND Operator
Task
Retrieve all students from Hyderabad who scored 85 or above.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
OR Operator
Task
Retrieve all students who are from Delhi or Mumbai.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
IN Operator
Task
Retrieve all students who are from Hyderabad, Delhi, or Chennai.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
NOT IN Operator
Task
Retrieve all students who are not from Hyderabad or Delhi.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
BETWEEN Operator
Task
Retrieve all students whose marks are between 80 and 90.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
LIKE Operator
Task
Retrieve all students whose names start with ‘K’.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
ILIKE Operator
Note: SQLite does not supportILIKE. TheLIKEoperator in SQLite is case-insensitive for ASCII characters by default.
Task
Retrieve all students whose names contain ‘ra’, ignoring case.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
ORDER BY
Ascending Order
Task
Retrieve all students ordered by marks in ascending order.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Descending Order
Task
Retrieve all students ordered by marks in descending order.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
DISTINCT
Task
Retrieve all unique cities.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
LIMIT
Task
Retrieve the top 5 students.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
OFFSET
Task
Skip the first 5 students and retrieve the next 5 students.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
first()
Task
Retrieve the first student.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
one()
Task
Retrieve the student whose name is Shreya.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
one_or_none()
Task
Retrieve the student whose name is Krishna.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Aggregate Functions
Aggregate functions perform calculations on multiple rows and return a single value.COUNT()
Task
Find the total number of students.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
SUM()
Task
Find the total marks of all students.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
AVG()
Task
Find the average marks.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
MIN()
Task
Find the minimum marks.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
MAX()
Task
Find the highest marks.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Multiple Aggregate Functions
Task
Retrieve the total students, average marks, minimum marks and maximum marks.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
SQL Functions (func)
SQLAlchemy provides the func object to call SQL database functions.
Instead of writing SQL functions as strings, they are accessed using func.
Import
General Syntax
SQL
SQLAlchemy
Common SQL Functions
Example
SQL (SQLite)
SQLAlchemy
How func Works
Where Can func Be Used?
func can be used with:
select()where()group_by()having()order_by()
Examples
Key Points
funcis used to call SQL database functions.- SQLAlchemy automatically converts
funcinto the corresponding SQL function. funcworks with all supported databases such as SQLite, MySQL, and PostgreSQL.- Aggregate functions like
COUNT(),SUM(),AVG(),MIN(), andMAX()are accessed throughfunc.
GROUP BY
TheGROUP BY clause groups rows that have the same values in one or more columns. It is commonly used with aggregate functions such as COUNT(), SUM(), AVG(), MIN(), and MAX().
Group by One Column
Task
Count the number of students in each city.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Count Students in Each Course
Task
Find the number of students enrolled in each course.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Average Marks by City
Task
Find the average marks scored by students in each city.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Maximum Marks in Each Course
Task
Find the highest marks scored in each course.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
HAVING
TheHAVING clause filters groups after GROUP BY.
Unlike WHERE, which filters individual rows, HAVING filters grouped results.
Students Count Greater Than 4
Task
Retrieve cities having more than 4 students.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
Average Marks Greater Than 85
Task
Retrieve cities whose average marks are greater than 85.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output
WHERE vs HAVING
Complete Example
Task
Retrieve the average marks for each city, considering only students with marks 80 or above, and display only cities whose average marks are greater than 85.SQL (SQLite)
SQLAlchemy Solution
SQLAlchemy Solution
Expected Output
Expected Output