Explore interactive code cells, execute DISTINCT unique value queries, test multi-column uniqueness, and run SQLAlchemy 2.0+ workflows directly in Google Colab.
View on GitHubSELECT DISTINCT(Column_name) FROM table
In our sample database my_db.db, we have student table. Each row ( student ) have one class. We will display the unique classes by using DISTINCT query.
q = "SELECT DISTINCT(class) FROM student"
try:
my_cursor = my_conn.execute(q)
for row in my_cursor:
print(row[0])
except sqlite3.Error as my_error:
print("error: ", my_error)
Output
Four
Three
Five
Six
Seven
Nine
Eight
We can add ORDER BY to this to display in alphabetical order. ( the change in query is shown here )
q = "SELECT DISTINCT(class) FROM student ORDER BY class"
q = "SELECT COUNT(DISTINCT(class)) FROM student"
Output
7
Using SQLAlchemy (2.0+ Context Manager) with WHERE condition
from sqlalchemy import create_engine, text
from sqlalchemy.exc import SQLAlchemyError
my_conn = create_engine("sqlite:///my_db.db")
q = text("SELECT DISTINCT(class) FROM student WHERE mark >= 80 ORDER BY class")
try:
with my_conn.connect() as connection:
r_set = connection.execute(q)
data = r_set.fetchall()
for row in data:
print(row[0])
print("No of records displayed : ", len(data))
except SQLAlchemyError as e:
print(e)
Output
Three
Five
Six
Seven
Four
No of records displayed : 5
q = "SELECT DISTINCT class, gender FROM student"
try:
my_cursor = my_conn.execute(q)
for row in my_cursor:
print(row[0], row[1])
except sqlite3.Error as my_error:
print("error: ", my_error)
Output
Four female
Three male
Four male
Five male
Six male
Six female
Seven female
Nine male
Nine female
Eight male
Seven male
Three female
q = "SELECT DISTINCT(class) FROM student WHERE class IS NOT NULL"
try:
my_cursor = my_conn.execute(q)
for row in my_cursor:
print(row[0])
except sqlite3.Error as my_error:
print("error: my_error)
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.