Docsity

SQL & Database Management Complete Notes + 100 Practice Queries, Lecture notes of Computer science

Toggle sidebar
bg1
Python Programming • Study Notes Page 1
SQL & DATABASE MANAGEMENT
Complete Notes + 100 Practice
Queries
Beginner → Exam Ready
A structured study package covering database fundamentals, SQL commands,
queries, relationships, normalization, transactions, practical exercises, MCQs and viva
preparation.
INCLUDES FOCUS
100 practice queries Theory + practical SQL
MCQs + viva questions Exam & lab preparation
Revision sheets Beginner-friendly explanations
Original study material. SQL syntax can vary slightly between MySQL, PostgreSQL, SQLite, SQL Server and Oracle; check the
dialect used in your course.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download SQL & Database Management Complete Notes + 100 Practice Queries and more Lecture notes Computer science in PDF only on Docsity!

SQL & DATABASE MANAGEMENT

Complete Notes + 100 Practice

Queries

Beginner → Exam Ready

A structured study package covering database fundamentals, SQL commands,

queries, relationships, normalization, transactions, practical exercises, MCQs and viva

preparation.

INCLUDES FOCUS

100 practice queries Theory + practical SQL MCQs + viva questions Exam & lab preparation Revision sheets Beginner-friendly explanations

Original study material. SQL syntax can vary slightly between MySQL, PostgreSQL, SQLite, SQL Server and Oracle; check the dialect used in your course.

TABLE OF CONTENTS

1. Database Fundamentals 2. DBMS & RDBMS 3. Relational Model & Tables 4. Keys & Constraints 5. SQL Command Categories 6. DDL Commands 7. DML Commands 8. SELECT & Filtering 9. Sorting & Limiting 10. Aggregate Functions 11. GROUP BY & HAVING 12. Joins 13. Subqueries 14. Set Operations 15. Functions & Expressions 16. Views & Indexes 17. Normalization 18. Transactions & ACID 19. ER Diagrams & Design 20. 100 Practice Queries 21. MCQs & Viva 22. Final Revision

Study method: understand the table → type the query → change conditions → test edge cases → explain the result in your own words.

Reduced duplication, controlled access, consistency, backup/recovery support, easier querying and centralized management.

Practice

Explain why a school might use a database instead of maintaining separate paper registers. Identify possible tables for students, classes and fees.

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

2. DBMS & RDBMS

A DBMS provides tools for storing and manipulating data. An RDBMS organizes data into related tables and commonly uses SQL to interact with those tables.

Relational databases represent entities using rows and attributes using columns. Relationships are commonly implemented with keys.

DBMS vs RDBMS

An RDBMS follows the relational model and supports relationships among tables. A DBMS is a broader term and can include systems that do not use the relational model.

Examples

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

3. Relational Model & Tables

A table contains rows (records) and columns (attributes). A column has a name and data type; a row represents one record.

Example Student table: student_id, name, class_name, marks. Each student row contains values for those attributes.

Create a table

CREATE TABLE students (

student_id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, class_name VARCHAR(20), marks DECIMAL(5,2) );

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

Data types

Common types include integer, decimal, character/varchar, date/time and boolean-like types depending on the database system.

Practice

Design tables for a library and a tuition center. List columns and suitable data types.

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

Key types

Primary key, candidate key, alternate key, foreign key and composite key are common relational concepts.

Practice

Explain why student_id is a better primary key than a student's name.

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

5. SQL Command Categories

SQL is used to define database structures, manipulate records, retrieve data and control transactions/permissions.

Common categories: DDL (CREATE, ALTER, DROP, TRUNCATE), DML (INSERT, UPDATE, DELETE), DQL (SELECT), DCL (GRANT, REVOKE), TCL (COMMIT, ROLLBACK, SAVEPOINT).

Remember

Different textbooks may classify SELECT under DQL or describe SQL categories slightly differently. Follow your course convention when answering exams.

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

6. DDL Commands

DDL changes database structure. CREATE creates objects; ALTER changes an existing object's definition; DROP removes an object; TRUNCATE removes rows while keeping the table structure in systems that support it.

Examples

CREATE TABLE courses (course_id INT PRIMARY KEY, title VARCHAR(100)); ALTER TABLE courses ADD COLUMN credits INT; DROP TABLE old_courses; TRUNCATE TABLE courses;

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

Caution

DROP can remove the table definition and data. TRUNCATE behavior and transaction support vary by database engine. Use destructive commands carefully.

Practice

Create a books table, add an author column, and describe what DROP and TRUNCATE would do.

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

Safety

Before UPDATE or DELETE, run a SELECT with the same WHERE condition to verify which rows will be affected.

Practice

Insert five student records, update one student's marks, then delete a selected record.

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

8. SELECT & Filtering

SELECT retrieves data. WHERE filters rows. Use comparison operators and logical conditions to narrow results.

Examples

SELECT name, marks FROM students;

SELECT * FROM students WHERE marks >= 80; SELECT * FROM students WHERE class_name = 'XI' AND marks >= 75;

NULL

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

9. Sorting & Limiting

ORDER BY sorts the result. ASC is ascending and DESC is descending. LIMIT is common in MySQL/PostgreSQL/SQLite, while other systems use different syntax for limiting rows.

Examples

SELECT name, marks FROM students ORDER BY marks DESC;

SELECT name, marks FROM students ORDER BY marks DESC LIMIT 5;

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.

Multiple columns

ORDER BY class_name ASC, marks DESC sorts first by class and then by marks within each class.

Practice

Find the top five marks and list students alphabetically within each class.

Quick review: Rewrite one query from this section without looking, then modify it with a different condition.