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.
  • Connecting Database in DBeaver
    1. Click on socket like icon just below File. (Shortcut: ctrl + shift + N).
    2. Click PostgreSQL and click Next.
    3. Fill in the connection details (Host, Port, Database, Username, Password).
    4. Click Test Connection. Note: For First Time Driver will be downloaded.
    5. Once connection is successful, click Finish to connect to database.
    6. Right click ondatabase name => SQL Editor => New SQL Script
    7. This opens SQL editor where we can write SQL queries.
    8. Create database as CREATE DATABASE db_name; -- Validate with GUI

SQL Commands

Categories of SQL Commands:
CategoryUsageExample
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

    Syntax:
    1. CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );
  • Common DataTypes: INT, SERIAL, FLOAT, DATE, VARCHAR, TEXT
  • Example:
    1. CREATE TABLE employees ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, salary FLOAT, created_at TIMESTAMP, is_vip BOOLEAN );

Sample Table for Mart

Sample Table to be created in SQL
Sample Table for SQL

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:
ConstraintDescription
PRIMARY KEYUniquely identifies each record in a table. Cannot be NULL / DUPLICATE.
FOREIGN KEYEnsures referential integrity between tables. Value must exist in referenced table.
NOT NULLEnsures that a column cannot have NULL value.
UNIQUEEnsures that all values in a column are unique.
CHECKEnsures that values in a column satisfy a specific condition.
DEFAULTProvides 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.
  • Inline Constraints Example:
    1. CREATE TABLE employees ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, salary FLOAT CHECK (salary > 0), created_at TIMESTAMP DEFAULT NOW() );
    Separate Constraints Example:
    1. 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));
    Add contraint to old table
    1. ALTER TABLE employees ADD CONSTRAINT pk_employees PRIMARY KEY (id);

Drop

  • Used to delete database, table or column.
  • Syntax:
    1. DROP DATABASE database_name;
    2. DROP TABLE table_name;
    Example:
    1. DROP TABLE IF EXISTS employees;
    2. DROP DATABASE IF EXISTS company_db;
    3. TRUNCATE TABLE employees; -- DROP table & re-run DDL.

Alter Table

  • Used to modify structure of existing table.
Common ALTER TABLE Operations:
OperationSyntax
Add ColumnALTER TABLE table_name ADD column_name datatype constraint;
Drop ColumnALTER TABLE table_name DROP COLUMN col_1, DROP COLUMN col_2;
Rename ColumnALTER TABLE table_name RENAME COLUMN old_name TO new_name;
Change Data TypeALTER TABLE table_name ALTER COLUMN column_name TYPE new_datatype;
Add ConstraintALTER TABLE table_name ADD CONSTRAINT constraint_name constraint_definition;
Drop ConstraintALTER TABLE table_name DROP CONSTRAINT constraint_name;
Rename TableALTER 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 INTO statement.
  • 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, ...);
  • Example:
    1. INSERT INTO employees (name, salary) VALUES ("Alice", 70000), ("Charlie", 60000);
  • Validate: SELECT * FROM employees;

Updating and Deleting Data

    UPDATE
    1. Used to modify existing records in a table.
    2. Syntax: UPDATE table_name SET col_1 = val_1, col_2 = val_2, ... , column_n = val_n WHERE condition;
    3. Example: UPDATE employees SET salary = 75000 WHERE name = "Alice";
    DELETE
    1. Used to remove records from a table.
    2. Syntax: DELETE FROM table_name WHERE condition;
    3. Example: DELETE FROM employees WHERE name = "Charlie";

Retrieving Data with SELECT

  • Data is retrieved from table with SELECT statement.
  • Syntax: SELECT column1, column2, ... FROM table_name WHERE condition ORDER BY column ASC|DESC LIMIT number;
  • Example:
    1. SELECT * FROM employees;
    2. SELECT name, salary FROM employees;
    3. SELECT * FROM employees LIMIT 10;
    4. SELECT DISTINCT department FROM employees;
    5. SELECT name, salary AS sl FROM employees ORDER BY 2 DESC, 1 LIMIT 5;

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.
  • Example:
    1. SELECT * FROM employees WHERE salary > 60000;
    2. SELECT * FROM employees WHERE department != "HR";
    3. SELECT * FROM employees WHERE salary >= 50000 AND salary < 80000;
    4. SELECT * FROM employees WHERE salary BETWEEN 50000 AND 80000;
    5. SELECT * FROM employees WHERE name = "Alice" OR name = "Bob";
    6. SELECT * FROM employees WHERE name IN ("Alice", "Bob");
    7. SELECT * FROM employees WHERE name LIKE "A%";
    8. SELECT * FROM employees WHERE name LIKE "%e";
    9. SELECT * FROM employees WHERE department IS NULL;
  • NOTE: Functions like LOWER, UPPER, LENGTH, ROUND, REPLACE, ABS, TRIM, CONCAT, IFNULL, CASE .. END etc. are available in SQL.

Read SQL table in Python

  • We use psycopg2 to connect & query PostgreSQL database in Python.
  • Installed as pip install psycopg2-binary.
  • Example:
    1. 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()

Write SQL table from Python

    Example:
    1. 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()

Practice QuestionsNot started

  1. Table Creation

    Question 1 of 5

      Connect your PostgreSQL database in DBeaver and:
      1. Create database named sample_db.
      2. Inside sample_db, create all tables from lecture slide with proper data types.
      3. Add records in each table as shown in lecture slide.
      4. Refresh database in DBeaver and validate table structure.
      5. Run SELECT * query on each table to validate inserted records.
  2. Table with Constraints

    Question 2 of 5

    • Drop tables created in previous exercise
    • Create all tables from lecture slide with below constraints:
      1. Customer - id => Serial, PRIMARY KEY - registered_date => DEFAULT value now - name => NOT NULL - phone => UNIQUE, CHECK: LENGTH = 10 - address => DEFAULT value NEPAL
      2. Employee - id => Serial, PRIMARY KEY - name => NOT NULL - joined_date => DEFAULT value now - department => CHECK: department IN ('HR', 'SALES', 'TECH') - salary => CHECK: salary > 0 - manager_id => FOREIGN KEY referencing employee(id)
      3. Product - id => Serial, PRIMARY KEY - name => NOT NULL - price => CHECK: price > 0
      4. Transaction - id => Serial, PRIMARY KEY - customer_id => FOREIGN KEY referencing customer(id) - cashier_id => FOREIGN KEY referencing employee(id) - product_id => FOREIGN KEY referencing product(id) - product_qty => CHECK: quantity > 0 - txn_date => DEFAULT value now
  3. Alter Table

    Question 3 of 5

    • Remove column discount_price from transaction.
    • Add column total_price in transaction with proper data type & constraint.
    • Rename column cashier_id to employee_id in transaction
    • Add mobile_number in employee making it unique.
    • Rename table customer to customer_info.
    • Add column brand in product with default value Generic.
    • Remove is_vip from customer_info, add customer_type with default value Regular.
  4. Restoring Data

    Question 4 of 5

    • Download DDL Script and DML scripts.
    • Open both scripts in text editor and understand the commands.
    • Run commands from DDL.sql to create tables, DML.sql to add data in your database.
    • Validate table structure and data with DBeaver GUI.
    • Make ER diagram of the database with DBeaver.
  5. Querying Data

    Question 5 of 5

    • Get first_name, last_name, phone, address of all customer & export as csv.
    • List all the products with price above 500.
    • List all the transaction that happen in month of June, 2025.
    • List Name, Address, Email of customer from Kathmandu / Pokhara. If there is no email, you have to display -. Sort result by name & address.
    • List HR employees who have already resigned from the mart.
    • Find top 5 expensive product. Exclude brands ElectroVision, BeanBrew.
    • List customer who uses Gmail.
    • Show all unique location from customer table.
    • Find the customer from Rasuwa who don't have email address.
    • Find the employee who don't have supervisor.
    • Find Name, Email and Phone number of customer from Dhangadi.
    • Find Name, Department of top 10 highest paid employee.
    • Get top 10 product that are low in stock excluding brand ComfortSit.
    • Find customer whose number doesn't start with 98.
    • Practice Questions from Hacker Rank.