RDBMS
Diploma – 3rd Semester, Computer Engineering
Unit 2
SQL In-built Functions
and Joins
Prepared by:
Bharti D. Mulchandani
Lecturer, Computer Engineering Department
R. C. Technical Institute, Ahmedabad
Course Outcome (CO)
Use joins and subqueries to retrieve and manipulate data from multiple tables.
BDM | RDBMS – Unit 2
1
2.1 SQL Operators — The DUAL Table
DESC dual; → Column: DUMMY VARCHAR2(1)
SELECT * FROM dual; → returns one row: 'X' (D = Dummy, X = dummy character)
SELECT 10 * 5 FROM dual; → 50 SELECT USER FROM dual; → current username
BDM | RDBMS – Unit 2
2
SQL Operators — Overview & Sample Table
Table 'person' — used throughout this unit:
SR | NAME | SURNAME | BDATE | CITY | BALANCE |
1 | bhagya | parikh | 15-JAN-10 | ahmedabad | 15000 |
2 | vihan | sagar | 22-FEB-12 | rangpur | 10000 |
3 | shreeja | parikh | 22-JUL-10 | rajkot | 5000 |
4 | samay | saxena | 10-AUG-18 | ahmedabad | 1000 |
5 | tushar | parmar | 13-NOV-90 | palanpur | 19000 |
6 | aarush | parikh | 15-JAN-15 | ahmedabad | 22000 |
7 | avani | solanki | 15-MAY-10 | ahmedabad | 200 |
8 | misha | sutariya | 22-DEC-12 | ahmedabad | 55000 |
9 | parth | parikh | 15-SEP-95 | rajkot | 5000 |
10 | aarav | solanki | 11-SEP-10 | ahmedabad | 1200 |
BDM | RDBMS – Unit 2
3
SQL Operators — Arithmetic
SELECT 5+3, 5-3, 5*3, 5/3 FROM dual; → 8 2 15 1.667
Example — add a bonus of 100 to every person's balance:
SELECT sr, name, balance "CURRENT BAL", balance+100 "NEW BAL" FROM person;
SR | NAME | CURRENT BAL | NEW BAL |
1 | bhagya | 15000 | 15100 |
4 | samay | 1000 | 1100 |
7 | avani | 200 | 300 |
BDM | RDBMS – Unit 2
4
SQL Operators — Relational (Comparison)
Example — balance greater than 15000:
SELECT * FROM person WHERE balance > 15000;
SR | NAME | CITY | BALANCE |
5 | tushar | palanpur | 19000 |
6 | aarush | ahmedabad | 22000 |
8 | misha | ahmedabad | 55000 |
Example — persons living in Rajkot:
SELECT * FROM person WHERE city = 'rajkot'; → shreeja, parth
BDM | RDBMS – Unit 2
5
SQL Operators — Logical (AND, OR, NOT)
Operator | Behaviour |
AND | TRUE only when both conditions are true |
OR | TRUE when at least one condition is true |
NOT | Reverses (negates) a condition's result |
SELECT name,city,balance FROM person WHERE city='ahmedabad' AND balance<5000; → samay, avani
SELECT name,city,balance FROM person WHERE city='ahmedabad' OR balance<5000; → samay, avani, misha, aarav
SELECT * FROM person WHERE NOT city = 'ahmedabad'; → all persons except samay, avani, misha
BDM | RDBMS – Unit 2
6
Range Operators — BETWEEN, IN, NOT IN
BETWEEN — checks a range (inclusive)
…ColumnName BETWEEN lower_limit AND upper_limit;
SELECT * FROM person WHERE balance BETWEEN 10000 AND 20000; → bhagya(15000), vihan(10000), tushar(19000)
IN — matches any value in a list (like 'multiple OR')
SELECT * FROM person WHERE city IN ('ahmedabad','rangpur','palanpur'); → 5 rows (vihan, samay, tushar, avani, misha)
NOT IN — opposite of IN
SELECT * FROM person WHERE city NOT IN ('ahmedabad','rangpur','palanpur'); → remaining 5 rows
BDM | RDBMS – Unit 2
7
Special Operators — LIKE (Pattern Matching)
SELECT name FROM person WHERE name LIKE 'a%'; -- starts with 'a' → aarush, avani, aarav
SELECT name FROM person WHERE name LIKE '_a%'; -- 'a' as 2nd letter → samay, aarush, parth, aarav
SELECT name,surname FROM person WHERE name LIKE '_____'; -- exactly 5 characters long
NAME | SURNAME |
vihan | sagar |
samay | saxena |
avani | solanki |
misha | sutariya |
aarav | solanki |
BDM | RDBMS – Unit 2
8
Special Operators — ALL & ANY
ALL — TRUE if the condition holds against every value from the subquery
…column operator ALL (subquery)
SELECT name,surname,balance FROM person
WHERE balance > ALL (SELECT balance FROM person WHERE surname='parikh'); → misha (55000)
ANY — TRUE if the condition holds against at least one value ('at least one match')
…column operator ANY (subquery)
SELECT name,balance FROM person
WHERE balance > ANY (SELECT balance FROM person WHERE surname='parikh'); → bhagya, vihan, tushar, aarush, misha
BDM | RDBMS – Unit 2
9
Special Operators — EXISTS
SELECT column_names FROM table_name WHERE EXISTS (subquery);
SELECT name, city FROM person
WHERE EXISTS (SELECT * FROM person WHERE city='ahmedabad'); → all 10 rows (subquery found a match)
BDM | RDBMS – Unit 2
10
Concatenation ( || ) & IS NULL
Concatenation operator ( || )
SELECT name || surname "FULL NAME" FROM person; → bhagyaparikh, vihansagar, shreejaparikh, …
IS NULL — find rows with a missing value
SELECT * FROM employees WHERE manager_id IS NULL;
EMP_ID | NAME | SALARY | MANAGER_ID |
101 | bhagya | 60000 | NULL |
104 | aarav | 6000 | NULL |
107 | dhairya | 18000 | NULL |
110 | samay | 41500 | NULL |
BDM | RDBMS – Unit 2
11
System Date: SYSDATE & SYSTIMESTAMP
SYSDATE
SELECT SYSDATE FROM dual;
SYSTIMESTAMP
SELECT SYSTIMESTAMP FROM dual;
→ 07-JUN-25 03.02.52.220000 PM +05:30
BDM | RDBMS – Unit 2
12
2.2 SQL In-built Functions — Overview
Aggregate Functions
Group functions — operate on many rows, return one summary value (MAX, MIN, SUM, AVG, COUNT)
Scalar Functions
Single-row functions — operate on one row/value, return one value per row (Numeric, Character, Date, Conversion)
BDM | RDBMS – Unit 2
13
Aggregate Functions — MAX & MIN
MAX() — returns the maximum value of a column
SELECT MAX(balance) FROM person; → 55000
SELECT MAX(name) FROM person; → vihan (works on character/date types too)
MIN() — returns the minimum value of a column
SELECT MIN(balance) FROM person; → 200
SELECT MIN(name) FROM person; → aarav
BDM | RDBMS – Unit 2
14
Aggregate Functions — SUM & AVG
SUM() — total of all values in a column
SUM(ColumnName) OR SUM([DISTINCT] ColumnName)
SELECT SUM(balance), SUM(DISTINCT balance) FROM person; → 133400, 133400
AVG() — average of all values in a column
AVG([DISTINCT] ColumnName)
SELECT AVG(balance), AVG(DISTINCT balance) FROM person; → 13340, 13340
BDM | RDBMS – Unit 2
15
Aggregate Functions — COUNT(*) & COUNT()
COUNT(*) — total rows in a table, including NULLs
SELECT COUNT(*) FROM person; → 10
COUNT(column) — total non-NULL values in a column
SELECT COUNT(city), COUNT(DISTINCT city) FROM person; → 10, 4
BDM | RDBMS – Unit 2
16
Scalar (Single-Row) Functions — Numeric
ABS(n) — absolute value (drops the negative sign)
SELECT ABS(-10), ABS(10), ABS(10.5) FROM dual; → 10, 10, 10.5
SQRT(n) — square root (n cannot be negative)
SELECT SQRT(25), SQRT(24) FROM dual; → 5, 4.899
BDM | RDBMS – Unit 2
17
Numeric Functions — POWER, MOD, CEIL, FLOOR
Function | Syntax | Description | Example → Result |
POWER | POWER(m, n) | Returns m raised to the power n | POWER(2,5) → 32 |
MOD | MOD(m, n) | Returns the remainder of m divided by n | MOD(17,5) → 2 |
CEIL | CEIL(n) | Smallest integer ≥ n (rounds up) | CEIL(10.2) → 11 |
FLOOR | FLOOR(n) | Largest integer ≤ n (rounds down) | FLOOR(10.6) → 10 |
BDM | RDBMS – Unit 2
18
Numeric Functions — ROUND vs TRUNC
ROUND(m, n) -- rounds m to n decimal places
SELECT ROUND(123.45,2), ROUND(123.45,-1), ROUND(123.45) FROM dual; → 123.45, 120, 123
TRUNC(m, n) -- truncates m to n decimal places (no rounding)
SELECT TRUNC(123.456,2), TRUNC(123.456), TRUNC(123.456,-2) FROM dual; → 123.45, 123, 100
Side-by-side comparison:
Value | ROUND(v,2) | TRUNC(v,2) |
456.789 | 456.79 | 456.78 |
135.356 | 135.36 | 135.35 |
153.456 (n=-2) | 200 | 100 |
BDM | RDBMS – Unit 2
19
Character Functions — LENGTH, LOWER/UPPER, INITCAP
SELECT LENGTH('hello') FROM dual; → 5
SELECT LOWER('BHAGYA'), UPPER('bhagya aarush') FROM dual; → bhagya, BHAGYA AARUSH
SELECT INITCAP('bharti d mulchandani') FROM dual; → Bharti D Mulchandani
BDM | RDBMS – Unit 2
20
Character Functions — TRIM, LTRIM, RTRIM
TRIM — removes leading/trailing spaces (or given characters) by default
SELECT TRIM(' aarush ') FROM dual; → 'aarush'
LTRIM(string, set) — removes matching characters from the LEFT
SELECT LTRIM('123BHAGYA','123') FROM dual; → BHAGYA
SELECT LTRIM('xyzxyzBHAGYA','xyz') FROM dual; → BHAGYA
RTRIM(string, set) — removes matching characters from the RIGHT
SELECT RTRIM('BHAGYA','AYPB') FROM dual; → BHAG
BDM | RDBMS – Unit 2
21
Character Functions — LPAD, RPAD, SUBSTR, INSTR
SELECT LPAD('aarush',10,'*') FROM dual; → '****aarush'
SELECT RPAD('aarush',10,'*') FROM dual; → 'aarush****'
SELECT SUBSTR('Bharti Mulchandani',3,3) FROM dual; → 'art' (start at pos 3, take 3 chars)
SELECT INSTR('aarush','r') FROM dual; → 3 (position of first 'r')
SELECT INSTR('aarush parikh','X') FROM dual; → 0 (not found)
BDM | RDBMS – Unit 2
22
Character Functions — REPLACE & TRANSLATE
REPLACE(string, from_string, to_string) — replaces a whole substring
SELECT REPLACE('abc12ef3','12','XY') FROM dual; → 'abcXYef3'
TRANSLATE(string, from_set, to_set) — replaces character-by-character
SELECT TRANSLATE('abc12ef3','1234','WXYZ') FROM dual; → 'abcWXefY'
BDM | RDBMS – Unit 2
23
Date Functions — ADD_MONTHS & MONTHS_BETWEEN
ADD_MONTHS(date, n) — adds (or subtracts, if n is negative) n months
SELECT ADD_MONTHS(sysdate,1), ADD_MONTHS(sysdate,-1) FROM dual;
MONTHS_BETWEEN(date1, date2) — months between two dates
SELECT MONTHS_BETWEEN(sysdate,'13-NOV-99') FROM dual; → ~293.5 (months since Nov 1999)
Example — calculate everyone's age from the person table:
SELECT name, bdate, FLOOR(MONTHS_BETWEEN(SYSDATE,bdate)/12) "AGE" FROM person;
BDM | RDBMS – Unit 2
24
Date Functions — ROUND & TRUNC
ROUND(date, unit) — rounds a date to the nearest day/month/year
SELECT ROUND(sysdate) FROM dual; -- rounds to nearest day
SELECT ROUND(sysdate,'year') FROM dual; -- rounds to nearest year (Jan 1)
TRUNC(date, unit) — truncates a date down to a specific unit
SELECT TRUNC(sysdate,'day') FROM dual; -- back to the start of the week
SELECT TRUNC(sysdate,'month') FROM dual; -- back to the 1st of the month
BDM | RDBMS – Unit 2
25
Conversion Functions — TO_NUMBER & TO_CHAR (number)
TO_NUMBER(string) — converts CHAR/VARCHAR2 into a NUMBER
SELECT TO_NUMBER('123'), TO_NUMBER('123.45') FROM dual; → 123, 123.45
TO_CHAR(n, format) — converts a NUMBER into formatted text
SELECT TO_CHAR(123456,'09,99,999') FROM dual; → '01,23,456'
SELECT TO_CHAR(12345,'99,999') FROM dual; → '12,345'
BDM | RDBMS – Unit 2
26
Conversion Functions — TO_CHAR (date) & TO_DATE
SELECT TO_CHAR(sysdate,'dd-mon-yyyy') FROM dual; → 17-sep-2023
SELECT TO_CHAR(sysdate,'day-mon-yyyy') FROM dual; → sunday-sep-2023
SELECT TO_DATE('13 november 2019','DD MONTH YYYY') FROM dual; → 13-NOV-19
Common format specifiers:
Format | Meaning | Format | Meaning |
MM | Month number (1–12) | DD | Day of month |
MON | 3-letter month (SEP) | DAY | Full day name |
MONTH | Full month name | YY / YYYY | 2 / 4-digit year |
BDM | RDBMS – Unit 2
27
2.3 SQL Clauses — ORDER BY
SELECT columnNames FROM table_name ORDER BY columnName [DESC];
SELECT name, balance FROM person ORDER BY balance; -- ascending
NAME | BALANCE |
avani | 200 |
samay | 1000 |
aarav | 1200 |
shreeja / parth | 5000 |
vihan | 10000 |
bhagya | 15000 |
tushar | 19000 |
aarush | 22000 |
misha | 55000 |
BDM | RDBMS – Unit 2
29
SQL Clauses — GROUP BY
SELECT columnNames, AggregateFunction(arg) FROM table_name WHERE condition GROUP BY columnNames;
SELECT surname, SUM(balance) "Total Balance" FROM person GROUP BY surname;
SURNAME | TOTAL BALANCE |
sutariya | 55000 |
parikh | 47000 |
parmar | 19000 |
saxena | 1000 |
solanki | 1400 |
sagar | 10000 |
BDM | RDBMS – Unit 2
30
SQL Clauses — HAVING
SELECT columnNames, ... FROM table_name WHERE condition GROUP BY columnNames HAVING condition;
SELECT surname, COUNT(surname) FROM person GROUP BY surname HAVING surname = 'parmar';
SURNAME | COUNT(SURNAME) |
parmar | 1 |
BDM | RDBMS – Unit 2
31
2.4 JOINS in SQL — Overview
Sample tables used throughout this section:
ID | NAME |
1 | bhagya |
2 | aarush |
3 | shreeja |
ID | CITY |
2 | ahmedabad |
3 | rajkot |
4 | ahmedabad |
Table: test1
Table: test2
BDM | RDBMS – Unit 2
32
JOINS — CROSS JOIN (Cartesian Product)
SELECT column1, ..., columnN FROM table1, table2;
SELECT * FROM test1, test2; -- 3 rows × 3 rows = 9 rows total
ID | NAME | ID | CITY |
1 | bhagya | 2 | ahmedabad |
1 | bhagya | 3 | rajkot |
2 | aarush | 2 | ahmedabad * |
3 | shreeja | 3 | rajkot * |
… | … | … | …(9 rows total) |
BDM | RDBMS – Unit 2
33
JOINS — INNER JOIN
SELECT column_names FROM table1, table2 WHERE table1.col OP table2.col;
SELECT column_names FROM table1 INNER JOIN table2 ON table1.col = table2.col;
SELECT test1.id, name, city FROM test1, test2 WHERE test1.id = test2.id;
ID | NAME | CITY |
2 | aarush | ahmedabad |
3 | shreeja | rajkot |
BDM | RDBMS – Unit 2
34
JOINS — OUTER JOIN (Overview)
Left Outer Join
All rows from the LEFT table + matches from the right; unmatched → NULL
Right Outer Join
All rows from the RIGHT table + matches from the left; unmatched → NULL
Full Outer Join
All rows from BOTH tables; unmatched sides → NULL
BDM | RDBMS – Unit 2
35
JOINS — LEFT OUTER JOIN
SELECT * FROM table1, table2 WHERE table1.col = table2.col(+);
SELECT * FROM table1 LEFT JOIN table2 ON table1.col = table2.col;
Example — left outer join of test1 and test2:
SELECT * FROM test1 left join test2 on test1.id = test2.id;
ID | NAME | ID | CITY |
2 | aarush | 2 | ahmedabad |
3 | shreeja | 3 | rajkot |
1 | bhagya | NULL | NULL |
BDM | RDBMS – Unit 2
36
JOINS — RIGHT OUTER JOIN
SELECT * FROM table1, table2 WHERE table1.col(+) = table2.col;
SELECT * FROM table1 RIGHT JOIN table2 ON table1.col = table2.col;
Example — right outer join of test1 and test2:
SELECT * FROM test1 right join test2 on test1.id = test2.id;
ID | NAME | ID | CITY |
2 | aarush | 2 | ahmedabad |
3 | shreeja | 3 | rajkot |
NULL | NULL | 4 | ahmedabad |
BDM | RDBMS – Unit 2
37
JOINS — FULL OUTER JOIN & Summary
SELECT * FROM t1 FULL JOIN t2 ON t1.col = t2.col;
ID | NAME | ID | CITY |
2 | aarush | 2 | ahmedabad |
3 | shreeja | 3 | rajkot |
1 | bhagya | NULL | NULL |
NULL | NULL | 4 | ahmedabad |
Join Type | Preserves |
LEFT OUTER | All of the left table's rows |
RIGHT OUTER | All of the right table's rows |
FULL OUTER | All rows from both tables |
BDM | RDBMS – Unit 2
38
JOINS — SELF JOIN
SELECT col1,... FROM table1 alias1, table1 alias2 WHERE condition;
Example table 'test_self' — br_no is a person's brother's row no., sis_no is their sister's row no.:
NO | NAME | BR_NO | SIS_NO |
1 | bhagya | — | 4 |
2 | aarav | 1 | 4 |
3 | aarush | 1 | 4 |
4 | avani | 1 | — |
SELECT a.name, b.name "Sister" FROM test_self a, test_self b WHERE a.sis_no = b.no;
→ aarush–avani, aarav–avani, bhagya–avani
BDM | RDBMS – Unit 2
39
2.5 SUB-QUERIES — Introduction
SELECT columnNames FROM tableName1
WHERE value ExpressionOperator (SELECT columnNames FROM tableName2 WHERE condition);
BDM | RDBMS – Unit 2
40
SUB-QUERIES — Example & Chain of Subqueries
Find the city of the person named 'aarush' (using test1, test2 from before):
SELECT city FROM test2 WHERE id IN (SELECT id FROM test1 WHERE name='aarush'); → ahmedabad
Chain of subqueries — three related tables:
teacher: id,name | teaches: name,subject | subject: subj,subid |
1 unp | unp – dbms | dbms – 3330703 |
2 bdp | bdp – os | os – 3330701 |
3 akp | akp – ds | ds – 3330705 |
Find the id of the faculty teaching subject 3330701:
SELECT id FROM teacher WHERE name = (SELECT name FROM teaches
WHERE subject = (SELECT subject FROM subject WHERE subid = 3330701)); → id = 2 (bdp)
BDM | RDBMS – Unit 2
41
SUB-QUERIES — EXISTS & Correlated Subqueries
EXISTS in a subquery:
SELECT subject FROM teaches t WHERE EXISTS (SELECT name FROM teacher WHERE name=t.name);
Correlated subquery — inner query re-runs for every outer row:
SELECT name, surname, balance FROM person P
WHERE balance IN (SELECT MAX(balance) FROM person WHERE surname = P.surname);
NAME | SURNAME | BALANCE |
vihan | sagar | 10000 |
aarush | parikh | 22000 |
misha | sutariya | 55000 |
aarav | solanki | 1200 |
BDM | RDBMS – Unit 2
42
2.6 SET Operators in SQL
Operator | What it does |
UNION | Combines both result sets, removing duplicates |
UNION ALL | Combines both result sets, keeping duplicates |
INTERSECT | Returns only rows common to both result sets |
MINUS | Returns rows in the first query but not the second |
BDM | RDBMS – Unit 2
43
SET Operators — UNION & UNION ALL
ID | NAME |
1 | bhagya |
2 | aarush |
3 | shreeja |
ID | NAME |
3 | shreeja |
4 | avani |
5 | aarav |
test1
test2
SELECT * FROM test1 UNION SELECT * FROM test2; → 5 rows (1,2,3,4,5) — no duplicate '3 shreeja'
SELECT * FROM test1 UNION ALL SELECT * FROM test2; → 6 rows — '3 shreeja' appears twice
BDM | RDBMS – Unit 2
44
SET Operators — INTERSECT & MINUS
INTERSECT — rows common to both queries
SELECT * FROM test1 INTERSECT SELECT * FROM test2; → 3 shreeja
MINUS — rows in the first query only, not the second
SELECT * FROM test1 MINUS SELECT * FROM test2; → 1 bhagya, 2 aarush
Practice example — collegeA, collegeB & hostelABCD:
SELECT * FROM collegeA INTERSECT SELECT * FROM hostelABCD;
-- collegeA students living in the hostel → a1 Aone, a3 Athree, a4 Afour
BDM | RDBMS – Unit 2
45
SET Operators — Summary
Set A = {1, 2, 3, 4} Set B = {3, 4, 5}
A UNION B
{1, 2, 3, 4, 5}
A UNION ALL B
{1,2,3,3,4,4,5}
A INTERSECT B
{3, 4}
A MINUS B
{1, 2}
BDM | RDBMS – Unit 2
46
Problem Solving Practice
Using the 'person' table from this unit, write SQL for:
Sample solution for Q3:
SELECT city, COUNT(*) FROM person GROUP BY city HAVING COUNT(*) > 1;
BDM | RDBMS – Unit 2
47
Quick Check: MCQs
1. Which is NOT an SQL arithmetic operator?
a) Modulus b) Addition c) Subtraction d) Unary
2. Which join returns all rows from both tables, with NULLs where there's no match?
a) INNER JOIN b) LEFT OUTER JOIN c) RIGHT OUTER JOIN d) FULL OUTER JOIN
3. Which SQL join type is also called an 'equi-join'?
a) OUTER JOIN b) INNER JOIN c) CROSS JOIN d) SELF JOIN
4. What does a CROSS JOIN return?
a) Only matching rows b) All row combinations c) Only left table rows d) Only unmatched rows
5. Which set operator keeps duplicate rows?
a) UNION b) UNION ALL c) INTERSECT d) MINUS
BDM | RDBMS – Unit 2
48
Thank You
Bharti D. Mulchandani
Lecturer, Computer Engineering Department
R. C. Technical Institute, Ahmedabad