Basic SQL
✕Database and SQL
- Relational Database: Organized collection of tables.
- Table: Collection of related data organized in rows(
record) and columns(field). - Analogy:
Table=>Excel Sheet,Database=>Excel Workbook. - Better performance, data integrity and security as compared to storing in files.
- RDBMS: Software that manages relational databases.
- Common RDBMS: MySQL, PostgreSQL, SQLite, Oracle, SQL Server (MS-SQL).
- SQL => Structured Query Language
- SQL: Code for searching, adding, modifying and removing data stored in table.
- DBeaver: Tool to Query various databases. Download from here & install.
- Postgres: Popular open-source RDBMS. Download from here & install.
Creating Database
- We will be using PostgreSQL database.
- PostgreSQL is a powerful popular open-source RDBMS.
- Click on
socketlike icon just below File. (Shortcut:ctrl + shift + N). - Click PostgreSQL and click Next.
- Fill in the connection details (
Host,Port,Database,Username,Password). - Click
Test Connection. Note: For First Time Driver will be downloaded. - Once connection is successful, click Finish to connect to database.
- Right click ondatabase name => SQL Editor => New SQL Script
- This opens SQL editor where we can write SQL queries.
- Create database as
CREATE DATABASE db_name;-- Validate with GUI
Connecting Database in DBeaver
SQL Commands
Categories of SQL Commands:
| Category | Usage | Example |
|---|---|---|
| DDL (Data Definition Language) | To define and manage database structure. | CREATE, ALTER, DROP |
| DML (Data Manipulation Language) | To manipulate data within tables. | INSERT, UPDATE, DELETE |
| DQL (Data Query Language) | To query and retrieve data from tables. | SELECT |
| DCL (Data Control Language) | To control access and permissions. | GRANT, REVOKE |
| TCL (Transaction Control Language) | To manage transactions. | COMMIT, ROLLBACK, SAVEPOINT |
Examples of different categories of SQL commands
Creating Tables
CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );- Common DataTypes:
INT,SERIAL,FLOAT,DATE,VARCHAR,TEXT CREATE TABLE employees ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, salary FLOAT, created_at TIMESTAMP, is_vip BOOLEAN );
Syntax:
Example:
Sample Table for Mart

Constraints
- Rule that helps to maintain data integrity and consistency in table.
- If data violates constraint, it will be rejected and error will be thrown.
Common SQL Constraints:
| Constraint | Description |
|---|---|
| PRIMARY KEY | Uniquely identifies each record in a table. Cannot be NULL / DUPLICATE. |
| FOREIGN KEY | Ensures referential integrity between tables. Value must exist in referenced table. |
| NOT NULL | Ensures that a column cannot have NULL value. |
| UNIQUE | Ensures that all values in a column are unique. |
| CHECK | Ensures that values in a column satisfy a specific condition. |
| DEFAULT | Provides a default value for a column when no value is specified. |
Examples of common SQL constraints
Creating table with Constraints
- When creating a table, constraints can be defined inline or as separate statements.
CREATE TABLE employees ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, salary FLOAT CHECK (salary > 0), created_at TIMESTAMP DEFAULT NOW() );CREATE TABLE employees ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, salary FLOAT, created_at TIMESTAMP DEFAULT NOW(), CONSTRAINT chk_sal CHECK (salary > 0));ALTER TABLE employees ADD CONSTRAINT pk_employees PRIMARY KEY (id);
Inline Constraints Example:
Separate Constraints Example:
Add contraint to old table
Drop
- Used to delete database, table or column.
DROP DATABASE database_name;DROP TABLE table_name;DROP TABLE IF EXISTS employees;DROP DATABASE IF EXISTS company_db;TRUNCATE TABLE employees;-- DROP table & re-run DDL.
Syntax:
Example:
Alter Table
- Used to modify structure of existing table.
Common ALTER TABLE Operations:
| Operation | Syntax |
|---|---|
| Add Column | ALTER TABLE table_name ADD column_name datatype constraint; |
| Drop Column | ALTER TABLE table_name DROP COLUMN col_1, DROP COLUMN col_2; |
| Rename Column | ALTER TABLE table_name RENAME COLUMN old_name TO new_name; |
| Change Data Type | ALTER TABLE table_name ALTER COLUMN column_name TYPE new_datatype; |
| Add Constraint | ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_definition; |
| Drop Constraint | ALTER TABLE table_name DROP CONSTRAINT constraint_name; |
| Rename Table | ALTER TABLE old_table_name RENAME TO new_table_name; |
Examples of common ALTER TABLE operations in PostgreSQL
Inserting Data
- Data is added in table with
INSERT INTOstatement. - Syntax:
INSERT INTO table_name (column1, column2, ...) VALUES (value_1_1, value_1_2, ...), (value_2_1, value_2_2, ...), ... (value_n_1, value_n_2, ...); INSERT INTO employees (name, salary) VALUES ("Alice", 70000), ("Charlie", 60000);- Validate:
SELECT * FROM employees;
Example:
Updating and Deleting Data
- Used to modify existing records in a table.
- Syntax:
UPDATE table_name SET col_1 = val_1, col_2 = val_2, ... , column_n = val_n WHERE condition; - Example:
UPDATE employees SET salary = 75000 WHERE name = "Alice"; - Used to remove records from a table.
- Syntax:
DELETE FROM table_name WHERE condition; - Example:
DELETE FROM employees WHERE name = "Charlie";
UPDATE
DELETE
Retrieving Data with SELECT
- Data is retrieved from table with
SELECTstatement. - Syntax:
SELECT column1, column2, ... FROM table_name WHERE condition ORDER BY column ASC|DESC LIMIT number; SELECT * FROM employees;SELECT name, salary FROM employees;SELECT * FROM employees LIMIT 10;SELECT DISTINCT department FROM employees;SELECT name, salary AS sl FROM employees ORDER BY 2 DESC, 1 LIMIT 5;
Example:
Filter Conditions with WHERE Clause
- WHERE clause is used to filter records based on specified conditions.
- Multiple conditions can be combined with AND / OR.
- Operators:
=,!=,<,>,<=,>=,LIKE,IN,BETWEEN. SELECT * FROM employees WHERE salary > 60000;SELECT * FROM employees WHERE department != "HR";SELECT * FROM employees WHERE salary >= 50000 AND salary < 80000;SELECT * FROM employees WHERE salary BETWEEN 50000 AND 80000;SELECT * FROM employees WHERE name = "Alice" OR name = "Bob";SELECT * FROM employees WHERE name IN ("Alice", "Bob");SELECT * FROM employees WHERE name LIKE "A%";SELECT * FROM employees WHERE name LIKE "%e";SELECT * FROM employees WHERE department IS NULL;- NOTE: Functions like
LOWER,UPPER,LENGTH,ROUND,REPLACE,ABS,TRIM,CONCAT,IFNULL,CASE .. ENDetc. are available in SQL.
Example:
Read SQL table in Python
- We use
psycopg2to connect & query PostgreSQL database in Python. - Installed as
pip install psycopg2-binary. import psycopg2 db_config = { "host": "localhost", "dbname": "your_db", "user": "your_user", "password": "your_password" } conn = psycopg2.connect(**db_config) cursor = conn.cursor() cursor.execute("SELECT * FROM employees;") results = cursor.fetchall() print(results) cursor.close() conn.close()
Example:
Write SQL table from Python
import psycopg2 db_config = { "host": "localhost", "dbname": "your_db", "user": "your_user", "password": "your_password" } conn = psycopg2.connect(**db_config) cursor = conn.cursor() # Create table cursor.execute("CREATE TABLE IF NOT EXISTS products (name TEXT, price REAL);") # Insert data data_to_add = [("Laptop", 999.99), ("Smartphone", 499.99), ("Headphones", 199.99)] cursor.executemany("INSERT INTO products (name, price) VALUES (%s, %s);", data_to_add) conn.commit() cursor.close() conn.close()
Example:
