Computer Science PPT
Computer Science PPT
Python
The time has come now to finally query our
“Student” table. Fetching the data from record is
as simple as inserting them. The execute method
uses the SQL command to get all the data from
the table.
SELECT Query
“Select” is the most commonly used statement in
SQL. The SELECT Statement in SQL is used to retrieve
or fetch data from a table in a database. The syntax
for using this statement is “Select * from table_name”
and all the table data can be fetched in an object in
the form of list of Tuples.
Examp
le import sqlite3
connection =
sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM
student")
print("\nfetch one:")
res = cursor.fetchone()
print(res)
Outpu
t fetch one:
(1, 'Akshay', 'B', 'M', 87.8,
'2001-12-12')
Displaying all records using fetchone()
Using while loop and fetchone() method we can display
all the records from a table.
Exampl import sqlite3
connection =
e sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM
student")
print("fetching all records one by
one:")
result = cursor.fetchone()
while result is not None:
Outpu fetching all records one by one:
print(result)
(1, 'Akshay', 'B', 'M', 87.8, '2001-12-
result = cursor.fetchone()
t 12')
(2, 'Aravind', 'A', 'M', 92.5, '2000-08-
17')
(3, 'BASKAR', 'C', 'M', 75.2, '1998-
05-17')
(4, 'SAJINI', 'A', 'F', 95.6, '2002-11-
Displaying using fetchmany()
Displaying specified number of records is done by
using fetchmany(). This method returns the next
number of rows (n) of the result set.
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM student")
print("fetching first 3 records:")
result = cursor.fetchmany(3)
print(*result,sep="\n") # * is used for unpacking a
tuple.
Outpu
t fetching first 3 records:
(1, 'Akshay', 'B', 'M', 87.8, '2001-12-
12')
(2, 'Aravind', 'A', 'M', 92.5, '2000-
08-17')
(3, 'BASKAR', 'C', 'M', 75.2, '1998-
CLAUSES IN SQL
• DISTINCT
• WHERE
• GROUP
BY
• ORDER
BY
• HAVING
SQL DISTINCT CLAUSE
The distinct keyword is helpful when there is need
of avoiding the duplicate values present in any
specific columns/table. When we use distinct
keyword only the unique values are fetched. In this
example we are going to display the different
grades scored by students from “student table”.
Exampl import sqlite3
connection = sqlite3.connect("Academy.db")
e cursor = connection.cursor()
cursor.execute("SELECT DISTINCT (Grade) FROM
student")
result = cursor.fetchall()
print(result)
Output ('F', 2)
('M',
5)
SQL ORDER BY Clause
The ORDER BY Clause can be used along with the SELECT statement to sort
the data of specific fields in an ordered way. It is used to sort the result-set
in ascending or descending order. In this example name and Rollno of the
students are displayed in alphabetical order of names.
Exampl
e
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT Rollno,sname FROM student Order
BY sname")
result = cursor.fetchall()
print(*result,sep="\n")
(1,
Outpu 'Akshay')
t (2,
'Aravind')
(3,
'BASKAR')
(6, 'PRIYA')
(4, 'SAJINI')
SQL HAVING Clause
Having clause is used to filter data based on the
group functions. This is similar to WHERE
condition but can be used only with group
functions. Group functions cannot be used in
WHERE Clause but can be used in HAVING clause.
Examp
le
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT GENDER,COUNT(GENDER)
FROM Student GROUP BY
GENDER HAVING COUNT(GENDER)>3")
result = cursor.fetchall()
co = [i[0] for i in cursor.description]
print(co)
print(result)
['gender', 'COUNT(GENDER)']
Outpu [('M', 5)]
THANK
YOU!
Presentation By Kanishka
and Vishwa Harini