SQL Server · Fundamentals

Database Fundamentals

Before writing SQL, it helps to picture how a relational database stores information. The ideas here apply to SQL Server, MySQL, PostgreSQL and every other relational database.

Beginner 5 min Lesson 1 of 31Examples run on SQL Server 2022 and Azure SQL Database.
0/31 in SQL Server
On this page · 5 min · Beginner

In simple words#

A database is a well-organised school office. Each register (table) holds one kind of record — students, classes, fees — in rows and columns, and registers refer to each other by roll number (keys).

Table
Rows (records) and columns (fields) about one thing.
Primary key
A unique ID for each row.
RDBMS
Relational database system, e.g. SQL Server.

Learning objectives#

  • Explain what a database and a DBMS are
  • Describe tables, rows and columns
  • Understand primary and foreign keys
  • Know what SQL is used for

Explanation#

A database is an organised collection of data. A database management system (DBMS) such as SQL Server is the software that stores it safely, lets many users read and change it at once, and recovers it after crashes.

In a relational database, data lives in tables. Each table describes one kind of thing — customers, products, orders. A column is one attribute (Name, City); a row is one record (one customer).

Every table should have a primary key: a column whose value uniquely identifies each row. A foreign key is a column that points to a primary key in another table, which is how tables are related — an order “belongs to” a customer.

SQL (Structured Query Language) is the language you use to create tables, insert and change data, and ask questions (queries). SQL Server’s dialect is called T-SQL.

Code example: Asking a database a question#

SQLAsking a database a question Illustrative
-- Which customers live in Pune?
SELECT CustomerId, Name, City
FROM Customers
WHERE City = 'Pune';
Expected output
CustomerId  Name   City
1           Asha   Pune
3           Meera  Pune

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. SELECT lists the columns you want back.
  2. FROM names the table to read.
  3. WHERE keeps only the rows that match the condition.
  4. The result is itself a small table — a result set.

Real-world use case#

An online shop keeps customers, products and orders in separate tables. When you view “My orders”, the website runs a query that joins those tables to build the page.

Common mistakes#

Storing everything in one big table

Repeating a customer’s address on every order wastes space and causes inconsistencies when the address changes. Split data into related tables.

Tables without a primary key

Without a unique key, duplicate rows creep in and individual rows are hard to update or reference.

Best practices#

  • Give every table a primary key
  • Name tables after the thing they store (Customers, Orders)
  • Store each fact once and relate tables with keys
  • Use the right data type for each column

Remember this#

Database → tables → rows (records) and columns (fields).

Primary key = unique roll number of each row.

Relational = tables linked by keys.

Practice exercise#

Your task: Write a query that returns the Name of every customer who lives in Delhi.

Show solution
SQL Illustrative
SELECT Name FROM Customers WHERE City = 'Delhi';

Mini quiz#

Database Fundamentals quizMultiple choice Question 1 of 2

In a table, what is a row?

Score so far: 0/0

Want more? Practise all SQL Server MCQs · SQL Server interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.