ExamSaathi learning library · Government exams · SBI PO and Junior Associates reasoning and computer aptitude (main examinations); section named in the 2026 advertisements, which give no topic list · Computer knowledge
Database basics: tables, records, keys and simple queries
Learn what a database and a DBMS are, how a relational table holds records and fields, how primary, candidate, composite and foreign keys work, and how to read short SQL statements, from CREATE and INSERT to SELECT with WHERE, ORDER BY and COUNT, on made-up bank tables.
Status: original pilot lesson. Teacher review is pending. It is not a complete course or official exam material.
You will learn to
- Explain what a database and a DBMS are and why a DBMS beats separate files, avoiding redundancy and keeping data consistent, shared, secure and backed up.
- Describe a relational database table by its records (rows, tuples), fields (columns, attributes), data types and schema.
- Identify the primary, candidate, composite and foreign keys of database tables, and the integrity rules that these keys enforce.
- Name CREATE, ALTER and DROP as DDL, INSERT, UPDATE and DELETE as DML, and SELECT as the query statement, writing each as a short SQL statement.
- Work out which rows an SQL query with SELECT, WHERE and ORDER BY returns, and what COUNT(*) gives, on a small database table.
Before you start
In the 2026 SBI PO advertisement, Reasoning & Computer Aptitude is one test in the objective part of the Phase-II Main Examination; the advertisement gives no list of topics for it.
What to know first:
1. Data are raw facts, such as a name, an account number or an amount in rupees.
2. A file keeps data on a storage device, and a program must open the file to read or change it.
3. This lesson writes a table in one line: its name, then its fields in brackets, such as Branch(branch_code, branch_name, city). A row is a bracket of values in the same order, such as (B01, Station Road, Pune). Every bank, branch, customer and amount here is made up.
Three lessons on this site lead into this one:
1. MS Office basics: an Excel worksheet keeps data in lettered columns and numbered rows, much as a database table does, but with looser rules.
2. Computer memory: data that must last are saved on secondary storage, such as a hard disk or SSD, with back-up copies kept apart.
3. Cyber security: strong passwords, updates and antivirus keep stored data safe from theft and malware, and that includes a database.
Databases, the DBMS and relational tables
A database is an organised collection of related data, such as a bank's customers, accounts and branches, kept so that any item can be found or changed quickly. A database management system (DBMS) is the software that creates and runs a database and serves its users and programs; MySQL, Oracle, PostgreSQL and Microsoft Access are examples.
Compared with separate files kept by each office, a DBMS gives:
1. Less duplication: each fact, such as a customer's address, can be stored just once.
2. Consistent data: a change made once is seen by all, so no old copy disagrees.
3. Shared access: many users and programs use the same data at the same time.
4. Security: each user gets only the rights the job needs, such as read-only.
5. Backup and recovery: data can be copied and restored after a failure.
In the relational model, the most common kind, data sit in tables, and a table is also called a relation. Each row is a record, or tuple, about one item, such as one account. Each column is a field, or attribute, such as balance. Every field has a data type: INT for whole numbers, VARCHAR(n) for text of up to n characters, CHAR(n) for text of fixed length n, DATE for dates written 'YYYY-MM-DD'. The number of fields is the degree of a table and the number of rows its cardinality. The schema is the design: table names, fields, data types, keys and links between tables.
Finding the keys of a table
A key picks out exactly one row of a table; a foreign key links two tables. Judge keys by rules, not samples: a repeat rules a field out; only a rule such as 'each account number is issued once' rules it in.
1. Candidate key: any field, or smallest group of fields, unique in every row and never NULL (no value). In Branch(branch_code, branch_name, city), branch_code is one; if no two branches in one city share a name, (branch_name, city) is another.
2. Primary key: the candidate the designer picks, here branch_code; it must be unique and must not be NULL. The rest are alternate keys.
3. Composite key: two or more fields used together when no single field is unique. In JointHolder(acc_no, cust_id), an account may have two holders and a customer two accounts: either field can repeat, but no pair may.
4. Foreign key: a field whose values come from another table's primary key. In Account(acc_no, branch_code, balance), branch_code refers to Branch: it may repeat, and each non-NULL branch_code must exist in Branch. JointHolder's fields are foreign keys to Account and Customer.
Integrity means data stay correct and consistent: with these keys declared, the DBMS refuses a row whose primary key repeats or is NULL, or whose non-NULL branch_code matches no branch. Normalisation organises tables, often by splitting them into linked tables, to reduce redundancy and update problems.
Simple SQL: DDL, DML and queries
SQL (Structured Query Language) is the language for working with a relational DBMS. Every statement ends with a semicolon, and text or dates go inside single quotes. Each line below is a separate example.
DDL (Data Definition Language) builds and changes structure:
CREATE TABLE Branch (branch_code CHAR(3) PRIMARY KEY, branch_name VARCHAR(30), city VARCHAR(20)); makes a table.
ALTER TABLE Branch ADD phone CHAR(10); adds a field.
DROP TABLE Branch; removes the table with all its rows.
DML (Data Manipulation Language) works on rows, here of Customer(cust_id, name, city):
INSERT INTO Customer VALUES ('C105', 'Meera Joshi', 'Pune'); adds a row.
UPDATE Customer SET city = 'Nagpur' WHERE cust_id = 'C105'; changes rows that pass the WHERE test.
DELETE FROM Customer WHERE cust_id = 'C105'; removes rows but keeps the table.
SELECT is the query statement; it is kept apart from the DML statements INSERT, UPDATE and DELETE, and it only reads rows:
SELECT name FROM Customer WHERE city = 'Pune' ORDER BY name;
FROM names the table. WHERE keeps only rows that pass a test made with =, <, >, <=, >= or <> (not equal; many DBMS also accept !=), combined with AND, OR or NOT. ORDER BY sorts the result in ascending order (A to Z, small to large) unless DESC follows. SELECT * shows every field.
SELECT COUNT(*) FROM Customer WHERE city = 'Pune'; returns one number: how many rows pass the test.
Common mistakes
1. Mixing up DELETE and DROP. DELETE FROM Account WHERE acc_no = 3104; removes that row and keeps the table; DROP TABLE Account; removes the table itself.
2. Mixing up ALTER and UPDATE. ALTER (DDL) changes the structure, such as adding a field; UPDATE (DML) changes values inside rows.
3. Leaving out WHERE. UPDATE Account SET balance = 0; changes every row, and DELETE FROM Account; removes every row.
4. Expecting a foreign key to be unique. The code B07 appears once for each account at branch B07; it only has to exist in Branch.
5. Picking a primary key that can repeat or be NULL, such as a holder's name or city.
6. Reading >= as >. WHERE balance > 50000 leaves out an account with exactly 50000; >= keeps it.
7. Dropping the quotes: WHERE city = Pune is wrong; write 'Pune'. Numbers take no quotes.
8. Forgetting that ORDER BY sorts in ascending order unless DESC is written.
9. Calling a row a field. A row is a record (tuple); a column is a field (attribute).
10. Keeping the same fact in two tables to be safe. That is redundancy, and the copies can drift apart.
A routine for database questions
1. Write each table in one line, name and fields, with its rows below in the same field order.
2. For a key, test each field, then pairs of fields: unique in every row and never NULL? A repeat rules a field out; only a stated rule rules it in.
3. For a link between tables, find the field that holds another table's primary key. That is the foreign key, and each non-NULL value in it must exist in that table.
4. Name each statement: CREATE, ALTER and DROP are DDL (structure); INSERT, UPDATE and DELETE are DML (rows); SELECT, kept apart from DML, is the query statement that only reads rows.
5. Run a SELECT by hand: take the table after FROM, tick the rows that pass WHERE, sort the ticked rows as ORDER BY says, then show the fields named after SELECT, or give the number of ticked rows for COUNT(*).
6. For UPDATE and DELETE, mark the rows that pass WHERE first; with no WHERE, every row is hit. Apply statements one at a time, in the order given.
7. Count the rows twice before you choose an option.
Worked examples
A made-up bank keeps every account in a single sheet, AccountSheet(acc_no, holder, branch_code, branch_city, balance), with balances in rupees and these rows: (3101, Ravi Kumar, B07, Nashik, 18000), (3102, Lata Iyer, B07, Nashik, 52000), (3103, Imran Shaikh, B09, Satara, 7500) and (3104, Pooja Desai, B07, Nasik, 30000). Find the redundancy, split the sheet into two linked tables, and name the primary key of each table and the foreign key.
- Step 1: Look for a fact typed more than once. The city of branch B07 sits in rows 3101, 3102 and 3104, once for every B07 account. That is redundancy, and it has already done harm: row 3104 says Nasik while the other two say Nashik, so the sheet is inconsistent.
- Step 2: A branch's city is a fact about the branch, not about an account, so give branches their own table: Branch(branch_code, branch_city), with the rows (B07, Nashik) and (B09, Satara). Each city is now stored once, and fixing a spelling means changing one row.
- Step 3: The account facts stay together in Account(acc_no, holder, branch_code, balance). The field branch_code stays in every account row, because it is the link to the branch.
- Step 4: Choose the primary keys. The bank issues each account number once, so acc_no is unique and never NULL in Account; branch_code plays the same part in Branch. The field holder cannot be a key, since two customers may share a name.
- Step 5: Account.branch_code takes its values from the primary key of Branch, so it is a foreign key. B07 may appear in many account rows, but an account at B08 is refused until B08 is added to Branch. Every account here must belong to a branch, so branch_code is also declared NOT NULL; without NOT NULL, a foreign key may be left NULL. In SQL, once Branch exists with branch_code as its primary key: CREATE TABLE Account (acc_no INT PRIMARY KEY, holder VARCHAR(30), branch_code CHAR(3) NOT NULL, balance INT, FOREIGN KEY (branch_code) REFERENCES Branch(branch_code));
Answer: Branch(branch_code, branch_city) with primary key branch_code, and Account(acc_no, holder, branch_code, balance) with primary key acc_no and foreign key branch_code, which refers to Branch. Each branch city is now stored only once.
A made-up bank's table Account(acc_no, holder, branch_code, acc_type, balance), with balances in rupees, has six rows: (4201, Neha Kulkarni, B12, Savings, 26000), (4202, Arjun Nair, B15, Current, 91000), (4203, Sana Qureshi, B12, Savings, 8500), (4204, Vikram Gill, B12, Current, 64000), (4205, Devika Menon, B15, Savings, 47000) and (4206, Rohan Patil, B12, Savings, 33000). Find the result of Query A: SELECT holder, balance FROM Account WHERE branch_code = 'B12' AND balance > 25000 ORDER BY balance DESC; and of Query B: SELECT COUNT(*) FROM Account WHERE acc_type = 'Savings';
- Step 1: FROM Account: begin with all six rows.
- Step 2: WHERE branch_code = 'B12' AND balance > 25000 keeps a row only if both tests pass. The B12 rows are 4201, 4203, 4204 and 4206, and 4203 fails the balance test, since 8500 is not above 25000. Rows 4202 and 4205 fail the branch test. Kept: 4201, 4204 and 4206.
- Step 3: ORDER BY balance DESC puts the largest balance first: 64000 (4204), then 33000 (4206), then 26000 (4201).
- Step 4: SELECT holder, balance shows only those two fields, so Query A gives three rows: Vikram Gill 64000, Rohan Patil 33000, Neha Kulkarni 26000.
- Step 5: Query B keeps the Savings rows, 4201, 4203, 4205 and 4206, and COUNT(*) turns them into one number, 4. It does not list the rows themselves.
Answer: Query A returns 3 rows, in this order: Vikram Gill (64000), Rohan Patil (33000) and Neha Kulkarni (26000). Query B returns the number 4.
A made-up bank's table Deposit(fd_no, holder, city, amount), with amounts in rupees, holds (701, Kiran Bose, Surat, 15000), (702, Asha Pillai, Vadodara, 60000), (703, Manoj Yadav, Surat, 9000), (704, Rekha Jain, Rajkot, 25000) and (705, Tarun Shah, Surat, 40000). These four statements run in order. S1: ALTER TABLE Deposit ADD nominee VARCHAR(30); S2: UPDATE Deposit SET amount = amount + 5000 WHERE city = 'Surat'; S3: DELETE FROM Deposit WHERE amount < 20000; S4: SELECT COUNT(*) FROM Deposit WHERE city = 'Surat'; Name the type of each statement and find the number that S4 returns.
- Step 1: S1 is DDL. It changes the structure by adding the field nominee, which is NULL (no value) in every row for now. No row is added or removed, so all 5 rows remain.
- Step 2: S2 is DML. Its WHERE picks the three Surat rows, and each gains Rs. 5000: 701 becomes 20000, 703 becomes 14000 and 705 becomes 45000. Rows 702 and 704 do not change.
- Step 3: S3 is DML, and it sees the new amounts. Only 703, now at 14000, is below 20000. Row 701 now holds exactly 20000, which is not below 20000, so it stays. One row goes and 4 remain.
- Step 4: S4 is the query, a SELECT. The Surat rows left are 701 and 705, so COUNT(*) returns 2.
- Step 5: The trap: running S3 on the old amounts would also delete 701 (15000), and S4 would then return 1. Statements act one after another, each on the table that the earlier ones left behind.
Answer: S1 is DDL; S2 and S3 are DML; S4 is a SELECT query, and it returns 2.
Practice checks
- Which of these SQL statements is a DDL statement?
- A made-up bank stores loan instalments in Instalment(loan_no, inst_no, due_date, amount). The instalments of every loan are numbered 1, 2, 3 and so on. Four of its rows are (L51, 1, 2027-01-05, 4200), (L51, 2, 2027-02-05, 4200), (L52, 1, 2027-01-05, 4200) and (L52, 2, 2027-02-05, 4200). Which of these is the right primary key?
- In a made-up bank, Branch(branch_code, city) has the rows (B21, Ajmer), (B22, Kota) and (B23, Udaipur). Account(acc_no, holder, branch_code, balance) has the primary key acc_no and the foreign key branch_code, which refers to Branch; it already holds (6101, Leela Rathore, B22, 14000) and (6102, Harsh Vyas, B21, 52000). Which INSERT will the DBMS refuse?
- A made-up bank's table Account(acc_no, holder, city, balance), with balances in rupees, has seven rows: (8101, Tanvi Rao, Mangaluru, 50000), (8102, Zoya Ahmed, Udupi, 72000), (8103, Gita Shetty, Mangaluru, 18000), (8104, Nikhil Pai, Udupi, 49500), (8105, Asif Mulla, Mangaluru, 50000), (8106, Meena Rai, Udupi, 125000) and (8107, Rahul Kamath, Mangaluru, 9000). What does SELECT COUNT(*) FROM Account WHERE balance >= 50000; return?
- Customer(cust_id, name, city) holds (C31, Bhavna Ghosh, Kolkata), (C32, Ayan Das, Howrah), (C33, Dipankar Roy, Kolkata), (C34, Charu Sen, Kolkata) and (C35, Esha Paul, Howrah). The query SELECT name FROM Customer WHERE city = 'Kolkata' ORDER BY name DESC; is run. Which name is in the first row of the result?
Sources checked
- State Bank of India, recruitment of Probationary Officers (advertisement CRPD/PO/2026-27/09): detailed advertisement — 8. Selection procedure: Phase-II Main Examination, objective test (PDF page 7): Reasoning and Computer Aptitude; the advertisement names sections, not topics; facts only
- NCERT, Computer Science Class XII, Chapter 8: Database Concepts — 8.2.1 Limitations of a File System ((B) Data Redundancy, (C) Data Inconsistency, (F) Controlled Data Sharing), 8.3 Database Management System (8.3.1 File System to DBMS, 8.3.2 Key Concepts in DBMS: (A) Database Schema), 8.4 Relational Data Model and 8.5 Keys in a Relational Database (8.5.1 Candidate Key, 8.5.2 Primary Key, 8.5.3 Composite Primary Key, 8.5.4 Foreign Key): why a DBMS beats separate files, tables, tuples, attributes, degree and cardinality, and the kinds of key; ideas only
- NCERT, Computer Science Class XII, Chapter 9: Structured Query Language (SQL) — 9.3.1 Data type of Attribute, 9.4 SQL for Data Definition (9.4.2 CREATE Table, 9.4.4 ALTER Table, 9.4.5 DROP Statement), 9.5 SQL for Data Manipulation (9.5.1 INSERTION of Records), 9.6 SQL for Data Query (9.6.1 SELECT Statement, 9.6.2 Querying using Database OFFICE: (D) WHERE Clause, (F) ORDER BY Clause), 9.7.1 Data Updation, 9.7.2 Data Deletion and 9.8.2 Aggregate Functions (COUNT): DDL and DML statements, queries and counting rows; ideas only
- Wikipedia: Relational database — 2 Relational model, 3 Keys, 6 Terminology, 9 Constraints (9.1 Primary key, 9.2 Foreign key) and 11 Normalization: rows as records or tuples, keys that link tables, entity and referential integrity, and normalisation as removing redundancy; ideas only