Searching Data and Patterns in SQL
Searching Data and Patterns in SQL
🏁 Introduction
Structured Query Language (SQL) is the backbone of querying and managing
data in relational databases. One of the essential operations is searching —
retrieving information that matches patterns or specific values. This tutorial
begins by creating sample tables and demonstrates pattern searching with
simple SQL commands. It also highlights the syntax differences between SQLite,
MySQL, Oracle, and PostgreSQL.
2. MySQL
3. Oracle
4. PostgreSQL
AUTO_INCREMENT
Use INTEGER Use sequences Use SERIAL or
Auto-Increment (with PRIMARY
PRIMARY KEY or IDENTITY GENERATED
KEY )
📋 Table: ENROLLED
📋 Table: MARKS
✅ Result:
sid name city
✅ Result:
sid name city
✅ Result:
sid name city
Amit Sharma
107 South Delhi
✅ Result:
sid name city
✅ Result:
sid name city
✅ Result:
sid course_code marks
101 CS101 78
102 CS101 85
103 CS102 69
107 CS103 88
108 CS101 75
109 CS104 90
110 CS101 60
✅ Result:
total_students
The IN and NOT IN operators are used in SQL to filter rows based on a list of
specific values.
🔹'South
Query 1: Find students from cities in 'Delhi', 'New Delhi',
Delhi'
✅ Result:
sid name city
✅ Result:
sid name city
🔗Multiple
Step 5: Finding Values by Combining Data from
Tables
✅ Result:
name course_code marks
✅ Result:
name marks
Amit Kumar 78
Amitesh Sharma 85
Amit Sharma 88
✅ Result:
name semester
AVG() – average
These are often used with GROUP BY to summarize data per group (e.g., per
student or per course).
✅ Result:
total_marks
768
✅ Result:
average_marks
69.82
✅ Result:
highest lowest
91 34
✅ Result:
course_code student_count
CS101 5
CS102 2
CS103 2
CS104 1
CS105 1
✅ Result:
course_code total average
CS104 90 90.00
CS105 34 34.00
101 78
102 85
103 69
104 91
105 42
106 56
107 88
108 75
109 90
110 94
🧠 Notes
GROUP BY is mandatory when using aggregation with non-aggregated
columns.
You can use HAVING to filter groups (e.g., show only courses with average >
70).
📋 Result:
name
Amit Kumar
Amitesh Sharma
Sneha Singh
Amit Sharma
Anita Sharma
Amrita Kumari
🔄 Alternate Query:
SELECT DISTINCT STUDENT.name
FROM STUDENT, MARKS
WHERE STUDENT.sid = MARKS.sid
AND MARKS.marks > (SELECT AVG(marks) FROM MARKS);
📋 Result:
marks
91
📋 Result:
name
Sneha Singh
🔄 Alternate Query:
SELECT STUDENT.name
FROM STUDENT, MARKS
WHERE STUDENT.sid = MARKS.sid
AND MARKS.marks = (SELECT MAX(marks) FROM MARKS);
✅ Main Query:
SELECT name
FROM STUDENT
📋 Result:
name
Amit Sharma
Sneha Singh
🔄 Alternate Query:
SELECT DISTINCT S2.name
FROM STUDENT S1, STUDENT S2, ENROLLED E1, ENROLLED E2
WHERE S1.name = 'Amit Sharma'
AND S1.sid = E1.sid
AND S2.sid = E2.sid
AND E1.course_code = E2.course_code;
📋 Result:
semester
Sem3
🔄 Alternate Query:
SELECT ENROLLED.semester
FROM STUDENT, MARKS, ENROLLED
WHERE STUDENT.sid = MARKS.sid
AND STUDENT.sid = ENROLLED.sid
ORDER BY MARKS.marks ASC
LIMIT 1;
🔁 Step 7.5:,Set
INTERSECT and
Operations —
EXCEPT
UNION ,
SQL allows you to combine the results of two or more SELECT statements using
set operations:
Operator Description
UNION Combines two result sets, removing duplicates
UNION ALL Combines two result sets, including duplicates
INTERSECT Returns only rows common to both result sets
EXCEPT / MINUS Returns rows in the first set but not in the second
ENROLLED
sid course_code semester
🔹table1. or ENROLLED
: Students who are either in the STUDENT
UNION
📋 Result:
✅ Combines and removes duplicates
🔹 2. UNION ALL : Include duplicates as well
📋 Result:
sid
🔹enrolled
4. EXCEPT / MINUS : Students in STUDENT but not
In Oracle:
In MySQL (alternative):
📋 Result:
sid
104
105
UNION
❌ Removes Combine two result sets
duplicates
UNION ALL ✅ Keeps duplicates Combine and preserve all results
INTERSECT
❌ Removes Find overlap between two result sets
duplicates
EXCEPT
❌ Removes Find rows present in the first, not in the
duplicates second
MARKS Table
sid course_code marks
101 CS101 78
102 CS102 85
103 CS101 90
106 CS103 88
🔹column)
1. NATURAL JOIN (only matching rows by common
SELECT *
FROM STUDENT
NATURAL JOIN MARKS;
📋 Result:
sid name city course_code marks
SELECT *
FROM STUDENT
INNER JOIN MARKS ON STUDENT.sid = MARKS.sid;
📋 Result:
sid name course_code marks
🔹student)
3. RIGHT JOIN (all marks entries, even if no
📋 Result:
sid name course_code marks
🔹where
4. FULL OUTER JOIN (all rows from both, match
possible)
📋 Result:
sid name city course_code marks
🔄JOINs):
Alternate Query (simulate using UNION of LEFT and RIGHT
UNION
NATURAL /
INNER
✅ if matched ✅ if matched ❌ excluded
LEFT JOIN ✅ all rows ✅ only if matched NULL if no marks
RIGHT JOIN ✅ only if matched ✅ all rows NULL if no student
🧾Concepts
Additional SQL Commands and
DESC STUDENT;
✅ Lists all columns in the STUDENT table with their data types.
⚠️ Irreversible!
👁️ 4. VIEW — Create a Virtual Table
A view is a saved SQL query that behaves like a table.
✅ Deletes rows with marks less than 40 from the MARKS table.
🧹DELETE)
6. TRUNCATE — Delete All Data (faster than
CHECKPOINT;
✅ This trigger automatically logs every new student added to the STUDENT table.
✅ Summary Table
Command Purpose Reversible? Notes
ALTER Modify table structure ✅ Add/remove columns
DESC Describe table structure ✅ Read-only
DROP
Delete table/view
permanently
❌ Deletes structure and data
ROLLBACK
Undo uncommitted
transaction
✅ Before COMMIT
CHECKPOINT
Flush in-memory data to
disk
✅ PostgreSQL specific
TRIGGER
Auto action on DML
events
✅ (drop) Useful for logging, audit,
validation