Database Query using SQL Formulas & Cheat Sheet
Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.
Core Concepts & Theoretical Foundations
- SQL Single-Row Functions: Operate on a single record and return one result per row. Divided into Numeric, String, and Date & Time functions.
- Numeric Functions: `POWER(m, n)`, `ROUND(n, d)`, `MOD(m, n)`, `SQRT(n)`, `TRUNCATE(n, d)`.
- String/Text Functions: `UCASE()`/`UPPER()`, `LCASE()`/`LOWER()`, `MID()`/`SUBSTRING()`/`SUBSTR()`, `LENGTH()`, `LEFT()`, `RIGHT()`, `INSTR()`, `LTRIM()`, `RTRIM()`, `TRIM()`, `CONCAT()`.
- Date and Time Functions: `NOW()`, `CURDATE()`, `DATE()`, `MONTH()`, `MONTHNAME()`, `YEAR()`, `DAY()`/`DAYOFMONTH()`, `DAYNAME()`.
- Aggregate/Group Functions: `MAX()`, `MIN()`, `AVG()`, `SUM()`, `COUNT(*)`, `COUNT(col)`. Operate on a set of rows and return a single scalar value.
- Group By and Having: `GROUP BY` groups rows having identical values; `HAVING` filters aggregated groups (used with group functions, whereas `WHERE` filters individual rows before grouping).
- Operations on Relations: Cartesian Product (Cross Join: $m \times n$ rows, $c_1 + c_2$ columns), Equi-Join (matching primary/foreign key values), Natural Join.
Chapter Formulas & Equations (13 entries)
| # | Topic / Formula Name | Mathematical Form & Equation | Conditions, Meaning & Board Tips |
|---|---|---|---|
| 1 | Numeric Power Function |
|
Returns the value of a number raised to the power of another number. |
| 2 | Numeric Round Function |
|
Rounds a numeric expression to d decimal places. If d < 0, rounds to integer positions to the left of the decimal. |
| 3 | Numeric Modulo Function |
|
Returns remainder of division. If n is 0, returns NULL. |
| 4 | String Substring Extraction Function |
|
Extracts substring starting at 1-based character position pos for specified length len. |
| 5 | String Search Location Function (INSTR) |
|
Returns index of first occurrence of target substring within source string; returns 0 if absent. |
| 6 | String Whitespace Trimming Functions |
|
Strips extraneous whitespace characters from text boundaries. |
| 7 | Date and Time Timestamp Functions |
|
Retrieves current system date and time from database server host. |
| 8 | Date Component Extraction Functions |
|
Extracts individual temporal components from date or timestamp literals. |
| 9 | Count Function Rules (COUNT(*) vs COUNT(col)) |
|
COUNT(*) counts total tuples irrespective of NULLs; COUNT(column_name) ignores rows with NULL. |
| 10 | Aggregate Grouping with HAVING Filter |
|
HAVING filters groups post-aggregation; WHERE filters individual records prior to grouping. |
| 11 | Cartesian Product Cardinality & Degree Formula |
|
Combining relation R1 of n1 rows and c1 columns with R2 of n2 rows and c2 columns yields n1*n2 rows and c1+c2 columns. |
| 12 | Equi-Join Query Syntax |
|
Joins two tables where joining attribute values are equal using equality operator. |
| 13 | SQL Query Execution Order |
|
The logical processing sequence followed by SQL query engines during evaluation. |
Key Scientific Diagrams & Concept Schematics: Database Query using SQL
Visual Reference: sql-functions-and-joins
Official NCERT board curriculum concept schematic and mathematical/scientific reference diagram.
Step-by-Step Worked Board Exam Examples
Example 1: Given table 'Student' with columns (RollNo, Name, Stream, Marks). Write SQL queries to: (a) display Stream and maximum Marks in each stream having average marks above 75, and (b) display the last 3 characters of Name for all students.
Step 1: For stream grouping and filtering group average, use GROUP BY Stream HAVING AVG(Marks) > 75.
Step 2: To get last 3 characters of Name, use RIGHT(Name, 3) or SUBSTR(Name, -3).
Example 2: What will be the output of the following MySQL queries? (i) SELECT ROUND(145.678, 2); (ii) SELECT INSTR('COMPUTER SCIENCE', 'PUT'); (iii) SELECT MONTHNAME('2026-10-02'); (iv) SELECT MOD(29, 5);
Step 1: ROUND(145.678, 2) rounds to 2 decimal places: 145.68.
Step 2: INSTR('COMPUTER SCIENCE', 'PUT') finds 'P' at index 4 (1-based): 4.
Step 3: MONTHNAME('2026-10-02') extracts month name: 'October'.
Step 4: MOD(29, 5) remainder of 29/5: 4.
Common Mistakes to Avoid in Exam
- Using WHERE instead of HAVING with aggregate functions (e.g. `WHERE AVG(salary) > 50000` causes an error; must use `HAVING AVG(salary) > 50000`).
- Assuming SQL indexes start at 0: in SQL, string index positions start at 1 (e.g. `SUBSTR('ABC', 1, 1)` yields 'A').
- Confusing `COUNT(*)` with `COUNT(column)`: `COUNT(*)` counts all records including NULLs; `COUNT(column)` ignores NULL values.
- Adding degrees instead of multiplying when calculating Cartesian product rows: Rows = R1 * R2, while Columns = C1 + C2.
Target Practice Questions
Q1. Table A has 4 rows and 3 columns; Table B has 5 rows and 2 columns. Find the cardinality and degree of their Cartesian Product.
Correct Answer: Cardinality (rows) = 4 * 5 = 20; Degree (columns) = 3 + 2 = 5.
Q2. Write a SQL query to display the current day name (e.g. Monday, Tuesday).
Correct Answer: SELECT DAYNAME(CURDATE());
Q3. What will be the result of SELECT LENGTH(TRIM(' PYTHON '))?
Correct Answer: 6 (TRIM strips outer spaces, leaving 'PYTHON' of length 6).
Frequently Asked Questions
Q. What is the difference between WHERE and HAVING clause in SQL?
WHERE filters individual records before grouping occurs and cannot contain aggregate functions. HAVING filters aggregated groups after GROUP BY and can contain aggregate functions like SUM, AVG, COUNT.
Q. What is the difference between NOW() and CURDATE()?
NOW() returns both the current date and time ('YYYY-MM-DD HH:MM:SS'), whereas CURDATE() returns only the current date ('YYYY-MM-DD').
Q. How does ROUND(345.67, -1) behave?
A negative second argument rounds to positions left of the decimal point: -1 rounds to the nearest ten, yielding 350. -2 rounds to nearest hundred.
Q. What is an Equi-Join?
An Equi-Join joins two tables based on equality comparison (=) between common columns (typically Primary Key and Foreign Key).