Skip to content

Informatics Practices · Ch 1 — Querying and SQL Functions

Introduction

1.1

Introduction

The Purpose of This Chapter

In Class XI, you learned the fundamentals of databases — how to create them in MySQL, populate them with data, and retrieve that data using SQL queries. This chapter builds directly on that foundation. You will now learn more advanced SQL commands that let you perform richer queries: using single-row and multiple-row functions, sorting records in ascending or descending order, grouping records based on criteria, and working with multiple tables in a single query.

What You Will Learn in This Chapter

The chapter is organised around these major topics:

  • Functions in SQL — both single-row functions (which work on one row at a time) and multiple-row functions (which work on groups of rows).
  • Group By in SQL — how to group records based on a criterion and apply functions to each group.
  • Operations on Relations — working with two or more tables in a query, including joins and set operations.
  • Using Two Relations in a Query — practical examples of combining data from multiple tables.

The CARSHOWROOM Database

To illustrate these ideas, the chapter introduces a database called CARSHOWROOM, with the schema shown in Figure 1.1. It has four relations:

  • INVENTORY — Stores details of each car in the showroom's inventory: Car ID, Car Name, Price, Model, Year of Manufacture, and Fuel Type.
  • CUSTOMER — Stores customer information: Customer ID, Customer Name, Customer Address, Phone number, and Email.
  • SALE — Records each sale transaction: Invoice Number, Car ID, Customer ID, Sale Date, Payment Mode, Employee ID of the salesperson, and Selling Price.
  • EMPLOYEE — Stores employee details: Employee ID, Employee Name, Date of Birth, Date of Joining, Designation, and Salary.

The records of these four relations are shown in Tables 1.1, 1.2, 1.3 and 1.4 respectively — the exact sample data used in every worked example and exercise in this chapter. Make sure you are comfortable with the structure of these four tables before moving on.

Figure 1.1Schema diagram of database CARSHOWROOM
Fig. 1.1 — Schema diagram of database CARSHOWROOM

Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.

The schema diagram in Figure 1.1 is a visual map of the CARSHOWROOM database. It shows four rectangular boxes, one for each table: INVENTORY, CUSTOMER, SALE, and EMPLOYEE. Inside each box, the table's column names are listed vertically.

For INVENTORY, the columns are: CarID, CarName, Price, Model, YearManufacture, and FuelType. CarID is marked as the primary key (usually underlined or labelled in the diagram). For CUSTOMER, the columns are: CustID, CustName, CustAdd, Phone, and Email, with CustID as the primary key. For SALE, the columns are: InvoiceNo, CarID, CustID, SaleDate, PaymentMode, EmpID, and SalePrice. InvoiceNo is the primary key here. For EMPLOYEE, the columns are: EmpID, EmpName, DOB, DOJ, Designation, and Salary, with EmpID as the primary key.

The diagram also draws foreign-key links as arrows or lines connecting the related columns between tables. Specifically:

  • A line connects CarID in SALE to CarID in INVENTORY (each sale refers to a car in inventory).
  • A line connects CustID in SALE to CustID in CUSTOMER (each sale is linked to a customer).
  • A line connects EmpID in SALE to EmpID in EMPLOYEE (each sale is handled by an employee).
Note

The diagram does not show any other relationships — for example, there is no direct link between CUSTOMER and EMPLOYEE, or between INVENTORY and EMPLOYEE. All connections go through the SALE table, which acts as a central junction.

What this teaches: The schema diagram is a blueprint of the database structure. It shows you at a glance which tables exist, what data each stores, and how they are related through primary and foreign keys. This is essential for writing correct SQL queries — especially when you need to join tables. For instance, to find the name of the customer who bought a particular car, you would need to join SALE with CUSTOMER using CustID, and SALE with INVENTORY using CarID. The diagram makes these paths obvious.

Table 1.1INVENTORY

mysql> SELECT * FROM INVENTORY;

CarIdCarNamePriceModelYearManufactureFueltype
D001Car1582613.00LXI2017Petrol
D002Car1673112.00VXI2018Petrol
B001Car2567031.00Sigma1.22019Petrol
B002Car2647858.00Delta1.22018Petrol
E001Car3355205.005 STR STD2017CNG
E002Car3654914.00CARE2018CNG
S001Car4514000.00LXI2017Petrol
S002Car4614000.00VXI2018Petrol

8 rows in set (0.00 sec)

Table 1.2CUSTOMER

mysql> SELECT * FROM CUSTOMER;

CustIdCustNameCustAddPhoneEmail
C0001AmitSahaL-10, Pitampura4564587852amitsaha2@gmail.com
C0002RehnumaJ-12, SAKET5527688761rehnuma@hotmail.com
C0003CharviNayyar10/9, FF, Rohini6811635425charvi123@yahoo.com
C0004GurpreetA-10/2, SF, MayurVihar3511056125gur_singh@yahoo.com

4 rows in set (0.00 sec)

Table 1.3SALE

mysql> SELECT * FROM SALE;

InvoiceNoCarIdCustIdSaleDatePaymentModeEmpIDSalePrice
I00001D001C00012019-01-24Credit CardE004613247.00
I00002S001C00022018-12-12OnlineE001590321.00
I00003S002C00042019-01-25ChequeE010604000.00
I00004D002C00012018-10-15Bank FinanceE007659982.00
I00005E001C00032018-12-20Credit CardE002369310.00
I00006S002C00022019-01-30Bank FinanceE007620214.00

6 rows in set (0.00 sec)

Table 1.4EMPLOYEE

mysql> SELECT * FROM EMPLOYEE;

EmpIDEmpNameDOBDOJDesignationSalary
E001Rushil1994-07-102017-12-12Salesman25550
E002Sanjay1990-03-122016-06-05Salesman33100
E003Zohar1975-08-301999-01-08Peon20000
E004Arpit1989-06-062010-12-02Salesman39100
E006Sanjucta1985-11-032012-07-01Receptionist27350
E007Mayank1993-04-032017-01-01Salesman27352
E010Rajkumar1987-02-262013-10-23Salesman31111

7 rows in set (0.00 sec)