Data manipulation through SQL - Formula Sheet
SQL Syntax and Query Structures
In database manipulation with SQL, syntax templates act as formulas. Below are the key statements and functions used with SQLite in Python:
1. Database Connection and Cursor Initialization
connection = sqlite3.connect(database_name)
- database_name: The file path of the database on disk or the keyword ':memory:' for RAM-based storage.
cursor = connection.cursor()
- cursor: The control structure used to execute statements and fetch results.
2. Database Schema Creation
CREATE TABLE table_name (column_1 data_type constraints, column_2 data_type, ...);
- table_name: Unique name of the relation.
- column_1, column_2: Names of individual fields.
- data_type: Data types such as INTEGER, VARCHAR, DECIMAL, DATE, float, etc.
- constraints: Restrictions such as PRIMARY KEY, NOT NULL, UNIQUE, CHECK, or DEFAULT.
3. Data Insertion Formula
INSERT INTO table_name (column_list) VALUES (value_list);
- column_list: Optional explicit list of destination columns.
- value_list: Commas-separated literals mapped to the respective columns.
4. Aggregate Functions
| Function | Purpose | Syntax Formula |
|---|---|---|
| COUNT() | Returns the total count of matching rows. | SELECT COUNT(column_name) FROM table_name; |
| AVG() | Computes the average of numerical column values. | SELECT AVG(column_name) FROM table_name; |
| SUM() | Calculates the total sum of numerical column values. | SELECT SUM(column_name) FROM table_name; |
| MAX() | Finds the maximum value in a column. | SELECT MAX(column_name) FROM table_name; |
| MIN() | Finds the minimum value in a column. | SELECT MIN(column_name) FROM table_name; |
More for this chapter
Book Back Questions10 textbook MCQs · solved
Additional MCQs15 extra MCQs · solved
Practice TestInteractive · instant score
Book Back TestTest yourself on the textbook set
Additional MCQ TestTest yourself on the extra set
Study NotesConcepts & methods
More chapters in Computer Science
View all
1 Function
2 Data Abstraction
3 Scoping
4 Algorithmic Strategies
5 Python -Variables and Operators
6 Control Structures
7 Python functions
8 Strings and String manipulation
9 Lists, Tuples, Sets and Dictionary
10 Python Classes and objects
11 Database Concepts
12 Structured Query Language (SQL)
13 Python and CSV files
14 Importing C++ programs in Python.
16 Data visualization using pyplot: line chart, pie chart and bar chart