⚡0 XP🔥0

Introduction to Databases and SQL

Lesson 1 of 10 ⏱ 4 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs SQL

What databases, DBMS and RDBMS are, tables, rows, columns, primary and foreign keys, the five categories of SQL commands, and the college practice database used in this course.

What is a database?#

A database is an organised collection of data that can be stored, searched and updated efficiently. Your bank account, a railway reservation, an Instagram post and your college marksheet all live in databases.

A DBMS (Database Management System) is the software that manages the database: it stores the data, answers queries, keeps data consistent and controls who can access what. Examples: MySQL, PostgreSQL, Oracle, Microsoft SQL Server, SQLite.

Most business data lives in relational databases (RDBMS), where data is kept in tables that are related to each other.

हिंदी में समझें

Database = व्यवस्थित तरीके से रखा गया data (जैसे college का पूरा record)। DBMS वो software है जो इस data को संभालता है - MySQL, Oracle, PostgreSQL आदि। Relational database में data tables में रहता है - हर table एक चीज़ के बारे में (students, courses), हर row एक record और हर column एक जानकारी (नाम, शहर, CGPA)।

Tables, rows and columns#

student_id name city dept_id cgpa
101 Aarav Gupta Bhopal 1 7.06
102 Diya Soni Indore 1 7.18
104 Sneha Gupta Indore 2 7.52
  • A table (relation) stores one type of entity - here, students.
  • A row (record, tuple) is one student.
  • A column (field, attribute) is one property, with a fixed data type (number, text, date).

Keys#

  • Primary key (PK) - uniquely identifies each row; cannot be NULL or duplicated. student_id in students.
  • Foreign key (FK) - a column that refers to the primary key of another table, creating a relationship. students.dept_id → departments.dept_id.
  • Candidate key - any column that could be the primary key (for example email, if unique).
  • Composite key - a primary key made of several columns together.

The college database used in this course#

Every query in this course runs on a practice database called college:

departments (dept_id PK, name, building, founded)
      ▲ 1                          ▲ 1
      │                            │
      │ many                       │ many
students (student_id PK, name, gender, city, dept_id FK, year, cgpa, email, admission_date, fee)
      ▲ 1
      │ many
enrollments (enroll_id PK, student_id FK, course_id FK, marks, grade)
      │ many
      ▼ 1
courses (course_id PK, title, dept_id FK, credits, semester)

faculty (faculty_id PK, name, dept_id FK, designation, salary, joining_date, reports_to FK → faculty)

It has 6 departments, 30 students, 13 courses, 11 faculty members and 65 enrollments. Run your first query:

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

SELECT * means "all columns", and FROM students names the table. The result is itself a table.

▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run

When a try-it contains several queries, each result table is shown in turn.

What is SQL?#

SQL (Structured Query Language, pronounced "S-Q-L" or "sequel") is the standard language for working with relational databases. You describe what you want; the database works out how to get it.

Category Purpose Commands
DQL - Data Query Language read data SELECT
DDL - Data Definition Language define structure CREATE, ALTER, DROP, TRUNCATE
DML - Data Manipulation Language change data INSERT, UPDATE, DELETE
DCL - Data Control Language permissions GRANT, REVOKE
TCL - Transaction Control Language transactions BEGIN, COMMIT, ROLLBACK, SAVEPOINT

SQL syntax basics#

  • Statements end with a semicolon ;.
  • Keywords are not case-sensitive (SELECT = select), but writing them in CAPITALS is the convention.
  • Text values go in single quotes: 'Indore'.
  • Comments: -- single line and /* multi-line */.
  • Extra spaces and new lines are ignored, so format queries for readability.
▶ Try it Yourselfdatabase: collegeSQL
Ctrl + Enter to run
Note

This course runs on SQLite, a small database engine that runs in your browser. The SQL you learn is standard and works in MySQL, PostgreSQL and SQL Server too; where a function differs (like LIMIT vs TOP, or date functions), the lesson points it out.

Summary#

  • A relational database stores data in related tables of rows and columns; a DBMS manages it.
  • Primary keys identify rows; foreign keys link tables.
  • SQL has five command families: DQL, DDL, DML, DCL, TCL.
  • SELECT * FROM table; shows a whole table.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Your first query

Show all columns of the departments table.

Database: college (see the tables in this lesson)
2

Pick columns

Show only the title and credits of every course in the courses table.

Database: college (see the tables in this lesson)

Quick Quiz

Q1.

In a relational database, data is stored in...

Q2.

What is a primary key?

Q3.

A foreign key...

Q4.

CREATE, ALTER and DROP belong to which category?

Q5.

Is SQL case-sensitive for keywords?