This chapter explores querying and SQL functions, detailing single row and multiple row functions, GROUP BY queries, and operations on multiple relations using SQL commands, illustrated with practical examples related to a CARSHOWROOM database.
In the realm of database management, understanding SQL querying and the various functions available is pivotal for effective data handling. This chapter expands on foundational knowledge gained in earlier classes regarding MySQL while introducing new SQL commands that facilitate complex database queries. The central focus is on enhancing data retrieval techniques and querying capabilities through practical applications.
The CARSHOWROOM database consists of four fundamental relations:
CarID, CarName, Price, Model, YearManufacture, and FuelType.CustID, CustName, CustAdd, Phone, and Email.InvoiceNo, CarID, CustID, SaleDate, PaymentMode, EmpID, and SalePrice.EmpID, EmpName, DOB, DOJ, Designation, and Salary.The structured organization of this data enables efficient querying capabilities and facilitates smooth operational queries regarding car sales and customer engagement.
SQL functions are categorized based on their applicability, focusing on single record processes (Single Row Functions) and multiple record processing (Aggregate Functions).
Single Row Functions, also known as Scalar Functions, operate on individual records and yield single values as output. These functions can be categorized into:
POWER(), ROUND(), MOD(), etc.UCASE(), LENGTH(), and substring functions like MID().NOW(), DATE(), and MONTH() to extract date components.SELECT ROUND(Price * 0.12, 1) AS GST FROM INVENTORY;
In contrast to Single Row Functions, Aggregate Functions operate on sets of records, processing them collectively. Common Aggregate Functions include:
The GROUP BY clause is essential when needing to categorize results based on similar values across specified columns. This grouping facilitates the application of aggregate functions across distinct categories.
SELECT CustID, COUNT(*) AS TotalPurchases FROM SALE GROUP BY CustID;
When dealing with multiple tables or relations, SQL enables querying through various JOIN operations which pull together tuples based on shared attributes:
Example use of JOIN:
SELECT U.UName, C.Size, C.Price FROM UNIFORM U JOIN COST C ON U.UCode = C.UCode;
SQL permits utilizing two or more relations within a singular query through:
Example of a JOIN statement:
SELECT * FROM EMPLOYEE E JOIN SALE S ON E.EmpID = S.EmpID;
Successfully querying and utilizing SQL functions enriches data management skills and paves the way for advanced database manipulations. Understanding these concepts is essential for performing nuanced data analyses, fostering a robust comprehension of SQL within the database field.