Basic SQL

1. Database and SQL

    1.1 🗄️ What is a Database?
    1. A database is an organized place where data is stored. In Data Science and software applications, databases help us store, search, update, and manage data safely.
    1.2 📊 Relational Database
    1. A relational database stores data in tables. A table has: Rows ➜ individual records Columns ➜ fields or properties
Employee Table
idnamesalary
1Alice70000
2Charlie60000
Sample table: employees
    1.3 📘 Excel Analogy
    1. A database is similar to an Excel workbook.
Database ConceptExcel Analogy
DatabaseExcel workbook
TableExcel sheet
RowRecord
ColumnField
Analogy of database and Excel
SQL Table vs Database
SQL Table vs Database
    1.4 ✅ Why Use a Database Instead of Files?
    1. Databases are better than normal files for many situations because they provide: 👉 Better performance 👉 Better data integrity 👉 Better security 👉 Easier searching and filtering 👉 Better support for multiple users
    1.5 🛠️ What is RDBMS?
    1. RDBMS means Relational Database Management System. It is software used to manage relational databases. Common RDBMS examples: - PostgreSQL - MySQL - SQLite - Oracle - SQL Server
    1.6 💬 What is SQL?
    1. SQL stands for Structured Query Language. SQL is used to: ✅ Create databases and tables ✅ Insert data ✅ Read data ✅ Update data ✅ Delete data 📌 SQL is the language we use to talk to databases.
    1.7 🐘 PostgreSQL and DBeaver
    1. For this lesson, we will use: PostgreSQL: A popular open-source relational database system. PostgreSQL provides official download pages for different operating systems. DBeaver: A database tool used to connect to and query different databases. DBeaver Community is described as a free, open-source database management tool that supports databases including PostgreSQL. Useful links: - Download PostgreSQL - Download DBeaver Community

2. Creating and Connecting a Database

    2.1 🐘 PostgreSQL Database
    1. We will use PostgreSQL as our database. PostgreSQL is useful for learning SQL because it is powerful, popular, and open-source.
    2.2 🔌 Connecting PostgreSQL in DBeaver
    1. Steps: 1. Open DBeaver 2. Click the New Database Connection icon. Shortcut: Ctrl + Shift + N 3. Select PostgreSQL 4. Click Next 5. Fill in connection details: Host, Port, Database, Username, Password 6. Click Test Connection 7. If the connection is successful, click Finish 8. Right-click the database name 9. Go to SQL Editor → New SQL Script 10. Write SQL queries in the SQL editor 📌 Note: DBeaver may download the PostgreSQL driver when you test the connection for the first time.
    2.3 Creating a Database
    1. CREATE DATABASE company_db; 📌 A database is the container where we create tables and store data.

3. SQL Command Categories

    3.1 📚 Types of SQL Commands
    1. SQL commands are grouped based on what they do. DDL commands are used to change table structure, such as creating tables, deleting tables, adding/removing fields, or renaming fields. DML commands are used to change data, such as adding new records, removing records, or changing values in records. DQL commands are used to read data from tables based on a search condition. DCL commands are used to control access and permissions.
SQL Command Categories
CategoryFull FormUsageExamples
DDLData Definition LanguageDefines database structureCREATE, ALTER, DROP
DMLData Manipulation LanguageChanges data in tablesINSERT, UPDATE, DELETE
DQLData Query LanguageReads data from tablesSELECT
DCLData Control LanguageControls access and permissionsGRANT, REVOKE
TCLTransaction Control LanguageManages transactionsCOMMIT, ROLLBACK, SAVEPOINT
SQL Command Categories
SQL Command Categories
SQL Command Categories

4. Creating Tables

    4.1 🧱 What is a Table?
    1. A table is an entity that stores related data in rows and columns.
Sample Table: employees
idnamesalaryjoined_dateis_active
1Alice700002027-01-15TRUE
2Charlie600002027-06-20FALSE
Sample table: employees
    4.2 Syntax
    1. CREATE TABLE table_name (column1 datatype,column2 datatype,column3 datatype );
4.3 Common PostgreSQL Data Types
Data TypeMeaningExample
INTWhole number25
SERIALAuto-incrementing integer1, 2, 3...
FLOATDecimal number75.5
DATEDate value2026-07-16
TIMESTAMPDate and time2026-07-16 10:30:00
VARCHAR(n)Text with length limitVARCHAR(255)
TEXTLong textDescription
BOOLEANTrue or falseTRUE, FALSE
Common PostgreSQL Data Types
    4.4 Example: Create Employees Table
    1. CREATE TABLE employees (id SERIAL PRIMARY KEY,name VARCHAR(255),salary FLOAT,joined_date DATE,is_active BOOLEAN ); 📌 CREATE TABLE creates the structure of a table. 📌 PRIMARY KEY is a constraint — explained in next section.

5. SQL Constraints

    5.1 🔒 What are Constraints?
    1. Constraints are rules applied to table columns. They help keep data: ✅ Correct ✅ Consistent ✅ Safe ✅ Meaningful If data breaks a constraint, PostgreSQL rejects it.
5.2 Common SQL Constraints
ConstraintMeaning
PRIMARY KEYUniquely identifies each row. Cannot be null or duplicate
FOREIGN KEYConnects one table to another table
NOT NULLColumn must have a value
UNIQUEValues must be unique
CHECKValues must satisfy a condition
DEFAULTProvides a default value if no value is given
SQL Constraints
SQL Constraints
    5.3 Inline Constraint Example
    1. CREATE TABLE employees (id SERIAL PRIMARY KEY,name VARCHAR(255) NOT NULL,salary FLOAT CHECK (salary > 0),created_at TIMESTAMP DEFAULT NOW() ); 📌 NOW() is a built-in function that returns the current date and time.
    5.4 Named Constraint Example
    1. CREATE TABLE employees (id SERIAL PRIMARY KEY,name VARCHAR(255) NOT NULL,salary FLOAT,created_at TIMESTAMP DEFAULT NOW(),CONSTRAINT chk_salary CHECK (salary > 0) );
    5.5 Add Constraint to Existing Table
    1. ALTER TABLE employees ADD CONSTRAINT chk_salary CHECK (salary > 0); 📌 Constraints protect the table from bad data.

6. DROP, TRUNCATE, and ALTER

    6.1 🗑️ DROP
    1. DROP deletes a database or table completely. DROP TABLE employees; Safer version: DROP TABLE IF EXISTS employees; Drop a database: DROP DATABASE IF EXISTS company_db; ⚠️ Warning: DROP removes the structure and data.
    6.2 🧹 TRUNCATE
    1. TRUNCATE removes all rows from a table but keeps the table structure. TRUNCATE TABLE employees; 📌 Simple difference: DROP TABLE — Deletes the table itself TRUNCATE TABLE — Deletes all rows but keeps the table
DROP vs DELETE vs TRUNCATE
DROP vs DELETE vs TRUNCATE
    6.3 🛠️ ALTER TABLE
    1. ALTER TABLE changes the structure of an existing table.
ALTER TABLE Operations
OperationSyntax
Add columnALTER TABLE table_name ADD column_name datatype;
Drop columnALTER TABLE table_name DROP COLUMN column_name;
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;

7. Inserting Data

    7.1 ➕ INSERT INTO
    1. INSERT INTO adds data into a table. We specify the table name, columns, and values to insert. It is okay not to provide values for all columns if they have default values or allow NULL. The sequence of columns and values must match.
    7.2 Syntax
    1. INSERT INTO table_name (column1, column2) VALUES(value1, value2),(value3, value4); 📌 value1 and value3 are inserted into column1 📌 value2 and value4 are inserted into column2
    7.3 Example
    1. INSERT INTO employees (name, salary) VALUES('Alice', 70000),('Charlie', 60000); ⚠️ PostgreSQL note: Text values should use single quotes, like 'Alice', not double quotes.
    7.4 Validate Inserted Data
    1. SELECT * FROM employees; 📌 Simple idea: Insert first, then verify using SELECT.

8. Updating and Deleting Data

    8.1 ✏️ UPDATE
    1. UPDATE modifies existing records. Syntax UPDATE table_name SET column_name = new_value WHERE condition;Example UPDATE employees SET salary = 75000 WHERE name = 'Alice';UPDATE employees SET salary = 80000, is_active = FALSE WHERE id = 1; ⚠️ Important: Always use WHERE carefully. Without WHERE, all rows may be updated.
    8.2 ❌ DELETE
    1. DELETE removes records from a table. Syntax DELETE FROM table_name WHERE condition;Example DELETE FROM employees WHERE name = 'Charlie'; ⚠️ Important: Without WHERE, all rows may be deleted.
SQL CRUD Operations
SQL CRUD Operations

9. Retrieving Data with SELECT

    9.1 🔍 SELECT
    1. SELECT is used to retrieve data from a table. We can choose specific columns, filter rows, sort results, and limit the number of rows returned.
    9.2 Basic Syntax
    1. SELECT column1, column2 FROM table_name WHERE condition ORDER BY column ASC LIMIT number; 📌 WHERE, ORDER BY, and LIMIT are optional depending on what we need.
    9.3 Examples
    1. Select all columns: SELECT * FROM employees; Select specific columns: SELECT name, salary FROM employees; Limit number of rows: SELECT * FROM employees LIMIT 10; Select unique values: SELECT DISTINCT department FROM employees; Rename output column: SELECT name, salary AS sl FROM employees; Sort results: SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 5; 📌 Simple idea: SELECT is used when we want to read data.

10. Filtering Data with WHERE

    10.1 🎯 WHERE Clause
    1. WHERE filters records based on conditions. It works like filtering in Python or NumPy.
10.2 Common Operators
OperatorMeaning
=Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
LIKEPattern matching
INMatch any value in a list
BETWEENValue inside a range
IS NULLMissing value
SQL Filter with WHERE Clause
SQL Filter with WHERE Clause
    10.3 Examples
    1. Salary greater than 60000: SELECT * FROM employees WHERE salary > 60000; Department not equal to HR: SELECT * FROM employees WHERE department != 'HR'; Salary between two values: SELECT * FROM employees WHERE salary >= 50000 AND salary < 80000; Using BETWEEN: SELECT * FROM employees WHERE salary BETWEEN 50000 AND 80000; 📌 Note: BETWEEN includes both boundary values. Name is Alice or Bob: SELECT * FROM employees WHERE name = 'Alice' OR name = 'Bob'; Using IN: SELECT * FROM employees WHERE name IN ('Alice', 'Bob'); Names starting with A: SELECT * FROM employees WHERE name LIKE 'A%'; Names ending with e: SELECT * FROM employees WHERE name LIKE '%e'; Department is missing: SELECT * FROM employees WHERE department IS NULL; 📌 WHERE helps us select only the rows we need.

11. Useful SQL Functions

    11.1 Common Functions
    1. SQL has useful functions to operate on text and numbers. We can convert case, find length, round numbers, replace text, and more. Some popular functions and their descriptions are listed below:
FunctionMeaning
LOWER()Convert text to lowercase
UPPER()Convert text to uppercase
LENGTH()Count characters
ROUND()Round numbers
REPLACE()Replace text
ABS()Absolute value
TRIM()Remove extra spaces
CONCAT()Join text
COALESCE()Replace null with another value
CASE WHEN ... THEN ... ENDIf-else logic in SQL
Common SQL Functions

12. Read SQL Table in Python

    12.1 🐍 Why Connect SQL with Python?
    1. In Data Science, we often read data from a database into Python for analysis. We can use psycopg2 to connect Python with PostgreSQL. Install it using: pip install psycopg2-binary
    12.2 Example: Read Data
    1. import psycopg2db_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() 📌 Simple idea: Python sends SQL queries to PostgreSQL and receives the result.
Python SQL Connection
Python SQL Connection

13. Write SQL Table from Python

    13.1 Example: Create Table and Insert Data
    1. import psycopg2db_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() 📌 conn.commit() saves the transaction, similar to the SQL COMMIT command.
Advertisement
Ad PlaceholderSlot: 7421026683

Practice QuestionsNot started

  1. Table Creation

    Question 1 of 6

    Patient Details
    idnameagegenderdiagnosis
    1John Doe45MaleDiabetes
    2Jane Smith30FemaleHypertension
    3Alice Johnson55FemaleAsthma
      Create table patient to hold data from the table above
      1. Use proper data type for each column.
      2. Insert records in the patient table as shown above.
      3. Run SELECT * query on the patient table to validate inserted records.
    Doctor Details
    idnamespecializationyears_of_experience
    1Dr. Emily BrownCardiology10
    2Dr. Michael GreenNeurology8
    3Dr. Sarah WhitePediatrics12
      Create table doctor to hold data from the table above
      1. Use proper data type for each column.
      2. Insert records in the doctor table as shown above.
      3. Run SELECT * query on the doctor table to validate inserted records.
  2. Table with Constraints

    Question 2 of 6

    • Create a table called customer with Fields, Data Type and Constraint as listed below:
    ColumnData TypeConstraint Detail
    idserialPRIMARY KEY
    registered_datetimestampDEFAULT value now
    namevarchar(100)NOT NULL
    phonechar(10)UNIQUE, CHECK: LENGTH = 10
    addresstextDEFAULT value NEPAL
    is_vipboolean
    • Create a table called employee with Fields, Data Type and Constraint as listed below:
    ColumnData TypeConstraint Detail
    idserialPRIMARY KEY
    namevarchar(100)NOT NULL
    joined_datetimestampDEFAULT value now
    departmentvarchar(50)CHECK: department IN ('HR', 'SALES', 'TECH')
    salaryDECIMAL(10, 2)CHECK: salary > 0
    is_activebooleanDEFAULT value TRUE
    manager_idintFOREIGN KEY referencing employee(id)
    • Create a table called product with Fields, Data Type and Constraint as listed below:
    ColumnData TypeConstraint Detail
    idserialPRIMARY KEY
    namevarchar(100)NOT NULL
    stockintCHECK: stock >= 0
    priceDECIMAL(10, 2)CHECK: price > 0
    • Create a table called transaction_details with Fields, Data Type and Constraint as listed below:
    ColumnData TypeConstraint Detail
    idserialPRIMARY KEY
    customer_idintFOREIGN KEY referencing customer(id)
    cashier_idintFOREIGN KEY referencing employee(id)
    product_idintFOREIGN KEY referencing product(id)
    product_qtyintCHECK: product_qty > 0
    txn_datetimestampDEFAULT value now
    discount_priceDECIMAL(10, 2)
  3. Inserting Data

    Question 3 of 6

    • On customer, employee, product, and transaction_details tables created in last exercise, insert records as shown in tables below. After insertion, run SELECT queries to validate that records in each table are as expected.
    Customer Data
    idregistered_datenamephoneaddressis_vip
    12025-01-15 10:30:00Alice Johnson9876543210Kathmandutrue
    22025-02-20 14:45:00Bob Smith9876543211Pokharafalse
    32025-03-10 09:15:00Charlie Brown9876543212Lalitpurtrue
    42025-04-05 11:00:00David Lee9876543213Biratnagarfalse
    Employee Data
    idnamejoined_datedepartmentsalaryis_activemanager_id
    1David Lee2025-01-10 08:00:00HR50000.00true
    2Eva Green2025-02-15 09:30:00SALES60000.00true1
    Product Data
    idnamestockprice
    1Laptop501200.00
    2Smartphone100800.00
    3Headphones200150.00
    4Monitor75300.00
    Transaction Details Data
    idcustomer_idcashier_idproduct_idproduct_qtytxn_datediscount_price
    111122025-06-01 10:00:00100.00
    231332025-06-03 14:15:0030.00
    321222025-06-05 16:20:0010.00
    421322025-06-07 13:50:0025.00
    541432025-06-09 10:40:0030.00
    632112025-06-10 11:55:0020.00
  4. Alter Table

    Question 4 of 6

      On tables created in last exercise, perform below operations:
      1. Remove column discount_price from transaction_details.
      2. Add column total_price in transaction_details with a proper data type and constraint.
      3. Rename column cashier_id to employee_id in transaction_details.
      4. Add mobile_number in employee making it unique.
      5. Rename table customer to customer_info.
      6. Add column brand in product with default value Generic.
      7. Remove is_vip from customer_info, add customer_type with default value Regular.
  5. Restoring Data

    Question 5 of 6

    • 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.
  6. Querying Data

    Question 6 of 6

      On tables created in last exercise Restoring Data, perform below queries:
      1. Get first_name, last_name, phone and address of all customers and export as CSV.
      2. List all products with price above 500.
      3. List all transactions that happened in June 2025.
      4. List Name, Address, and Email of customers from Kathmandu or Pokhara. If there is no email, display -. Sort result by name & address.
      5. List HR employees who have already resigned from the mart.
      6. Find the top 5 most expensive products. Exclude brands ElectroVision, BeanBrew.
      7. List customers who use Gmail.
      8. Show all unique locations from the customer table.
      9. Find customers from Rasuwa who do not have an email address.
      10. Find employees who do not have a supervisor.
      11. Find Name, Email, and Phone number of customers from Dhangadi.
      12. Find Name and Department of the top 10 highest-paid employees.
      13. Get the top 10 products that are low in stock, excluding the brand ComfortSit.
      14. Find customers whose number does not start with 98.
Advertisement
Ad PlaceholderSlot: 5413242224