[('B',), ('A',), ('C',), ('D',)] Without the keyword “distinct” in the above example displays records instead of , since in the original table there are actually records and some are with the duplicate values. . . .
SQL WHERE CLAUSE The WHERE clause is used to extract only those records that fulfill a specified condition. In this example we are going to display the different grades scored by male students from “student table” 12th Computer Chapter - - Data Manipulation Through SQL import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT DISTINCT (Grade) FROM student where gender='M'") result = cursor.fetchall() print(*result,sep="\n") OUTPUT ('B',) ('A',) ('C',) ('D',) . . .
SQL Group By Clause The SELECT statement can be used along with GROUP BY clause. The GROUP BY clause groups records into summary rows. It returns one records for each group. It is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
The following example count the number of male and female from the student table and display the result. Example . . .
- import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT gender,count(gender) FROM student Group BY gender") result = cursor.fetchall() print(*result,sep="\n") OUTPUT ('F', ) ('M', ) . . . 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 12th Computer Chapter - - Example . . .
- 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") OUTPUT ( , 'Akshay') ( , 'Aravind') ( , 'BASKAR') ( , 'PRIYA') ( , 'SAJINI') ( , 'TARUN') ( , 'VARUN') . . . 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