Skip to main content

Select Queries

This chapter demonstrates how SQL queries are translated into SQLAlchemy ORM queries. Each concept follows the same structure:
  1. Task
  2. SQL (SQLite)
  3. SQLAlchemy Solution
  4. Expected Output

Preparation

Student Model

File: models.py

Sample Data

File: main.py

Basic SELECT

Task

Retrieve all students.

SQL (SQLite)

WHERE Clause

Task

Retrieve all students whose age is 21 or above.

SQL (SQLite)

Comparison Operators

Greater Than (>)

Task

Retrieve all students whose marks are greater than 90.

SQL (SQLite)

Less Than (<)

Task

Retrieve all students whose age is less than 20.

SQL (SQLite)

Greater Than or Equal To (>=)

Task

Retrieve all students whose marks are 85 or above.

SQL (SQLite)

Less Than or Equal To (<=)

Task

Retrieve all students whose age is 20 or below.

SQL (SQLite)

Equal To (==)

Task

Retrieve all students from Hyderabad.

SQL (SQLite)

Not Equal To (!=)

Task

Retrieve all students who are not enrolled in the Python course.

SQL (SQLite)

AND Operator

Task

Retrieve all students from Hyderabad who scored 85 or above.

SQL (SQLite)

OR Operator

Task

Retrieve all students who are from Delhi or Mumbai.

SQL (SQLite)

IN Operator

Task

Retrieve all students who are from Hyderabad, Delhi, or Chennai.

SQL (SQLite)

NOT IN Operator

Task

Retrieve all students who are not from Hyderabad or Delhi.

SQL (SQLite)

BETWEEN Operator

Task

Retrieve all students whose marks are between 80 and 90.

SQL (SQLite)

LIKE Operator

Task

Retrieve all students whose names start with ‘K’.

SQL (SQLite)

ILIKE Operator

Note: SQLite does not support ILIKE. The LIKE operator in SQLite is case-insensitive for ASCII characters by default.

Task

Retrieve all students whose names contain ‘ra’, ignoring case.

SQL (SQLite)

ORDER BY

Ascending Order

Task

Retrieve all students ordered by marks in ascending order.

SQL (SQLite)

Descending Order

Task

Retrieve all students ordered by marks in descending order.

SQL (SQLite)

DISTINCT

Task

Retrieve all unique cities.

SQL (SQLite)

LIMIT

Task

Retrieve the top 5 students.

SQL (SQLite)

OFFSET

Task

Skip the first 5 students and retrieve the next 5 students.

SQL (SQLite)

first()

Task

Retrieve the first student.

SQL (SQLite)

one()

Task

Retrieve the student whose name is Shreya.

SQL (SQLite)

one_or_none()

Task

Retrieve the student whose name is Krishna.

SQL (SQLite)

Aggregate Functions

Aggregate functions perform calculations on multiple rows and return a single value.

COUNT()

Task

Find the total number of students.

SQL (SQLite)

SUM()

Task

Find the total marks of all students.

SQL (SQLite)

AVG()

Task

Find the average marks.

SQL (SQLite)

MIN()

Task

Find the minimum marks.

SQL (SQLite)

MAX()

Task

Find the highest marks.

SQL (SQLite)

Multiple Aggregate Functions

Task

Retrieve the total students, average marks, minimum marks and maximum marks.

SQL (SQLite)

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

Counts the number of rows.
Calculates the total marks.
Calculates the average marks.
Returns the minimum marks.
Returns the maximum marks.
Converts the name to lowercase.
Converts the name to uppercase.
Returns the length of the student’s name.

Key Points

  • func is used to call SQL database functions.
  • SQLAlchemy automatically converts func into the corresponding SQL function.
  • func works with all supported databases such as SQLite, MySQL, and PostgreSQL.
  • Aggregate functions like COUNT(), SUM(), AVG(), MIN(), and MAX() are accessed through func.

GROUP BY

The GROUP 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)

Count Students in Each Course

Task

Find the number of students enrolled in each course.

SQL (SQLite)

Average Marks by City

Task

Find the average marks scored by students in each city.

SQL (SQLite)

Maximum Marks in Each Course

Task

Find the highest marks scored in each course.

SQL (SQLite)

HAVING

The HAVING 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)

Average Marks Greater Than 85

Task

Retrieve cities whose average marks are greater than 85.

SQL (SQLite)

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)