SQL and database basics for beginners

Every BSCS and BSIT student studies databases, and SQL is the language used to work with them. This beginner guide covers database concepts, keys, the most important SQL commands, joins and normalisation, with examples based on a simple student table.

What Is a Database and a DBMS?

A database is an organised collection of related data. A DBMS (Database Management System) is software that stores, retrieves and manages that data, for example MySQL, PostgreSQL, Microsoft SQL Server, Oracle and MS Access. Most of these are relational databases, where data is stored in tables made of rows (records) and columns (fields).

Keys in a Relational Database

  • Primary key: uniquely identifies each row, cannot be NULL or repeated (e.g. RollNo).
  • Foreign key: a column that refers to the primary key of another table, creating a relationship.
  • Candidate key: any column (or set) that could serve as the primary key.
  • Composite key: a primary key made of two or more columns.

Types of SQL Commands

TypePurposeCommands
DDL (Data Definition)Define structureCREATE, ALTER, DROP, TRUNCATE
DML (Data Manipulation)Change dataINSERT, UPDATE, DELETE
DQL (Data Query)Read dataSELECT
DCL (Data Control)PermissionsGRANT, REVOKE
TCL (Transaction Control)TransactionsCOMMIT, ROLLBACK

Create a Table and Insert Data

CREATE TABLE Students (
    RollNo   INT PRIMARY KEY,
    Name     VARCHAR(50) NOT NULL,
    Program  VARCHAR(20),
    CGPA     DECIMAL(3,2)
);

INSERT INTO Students (RollNo, Name, Program, CGPA)
VALUES (101, 'Ali', 'BSCS', 3.45),
       (102, 'Sara', 'BSIT', 3.80),
       (103, 'Usman', 'BSCS', 2.95);

SELECT: Reading Data

-- all columns, all rows
SELECT * FROM Students;

-- BSCS students with CGPA above 3.0, highest first
SELECT Name, CGPA
FROM Students
WHERE Program = 'BSCS' AND CGPA > 3.0
ORDER BY CGPA DESC;

UPDATE and DELETE

UPDATE Students SET CGPA = 3.10 WHERE RollNo = 103;
DELETE FROM Students WHERE RollNo = 102;

Warning: without a WHERE clause, UPDATE and DELETE affect every row in the table.

Aggregate Functions and GROUP BY

SELECT Program, COUNT(*) AS Total, AVG(CGPA) AS AvgCGPA
FROM Students
GROUP BY Program
HAVING COUNT(*) > 1;

Common aggregate functions: COUNT(), SUM(), AVG(), MIN(), MAX(). Use WHERE to filter rows before grouping and HAVING to filter groups after grouping.

Joins

Suppose a second table Results(RollNo, Course, Grade) has RollNo as a foreign key.

SELECT s.Name, r.Course, r.Grade
FROM Students s
INNER JOIN Results r ON s.RollNo = r.RollNo;
  • INNER JOIN: only rows that match in both tables.
  • LEFT JOIN: all rows from the left table, plus matches from the right (NULL if none).
  • RIGHT JOIN: all rows from the right table, plus matches from the left.
  • FULL OUTER JOIN: all rows from both tables.

Normalisation in Brief

  • 1NF: each cell holds a single (atomic) value; no repeating groups.
  • 2NF: in 1NF and every non-key column depends on the whole primary key (no partial dependency).
  • 3NF: in 2NF and no non-key column depends on another non-key column (no transitive dependency).

Normalisation reduces duplicate data and prevents update, insert and delete anomalies.

Practice Tasks

  1. Show the names of all BSIT students.
  2. Find the highest CGPA in the table.
  3. Count how many students are in each program.
  4. Increase the CGPA of roll number 101 to 3.50.

Check your knowledge with the free SQL MCQ practice test. You can also practise SQL online for free at W3Schools SQL.

FAQ

What is the difference between DELETE, TRUNCATE and DROP?

DELETE removes selected rows (can use WHERE), TRUNCATE removes all rows but keeps the table, and DROP deletes the whole table including its structure.

What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY.


0 comments:

Post a Comment

 
Top