Week 6
Week 6
Pattern Matching
1. Using the LIKE Operator
The LIKE operator is used to search for a specified pattern in a column.
Basic Syntax:
SELECT column1 FROM table_name WHERE column_name LIKE 'pattern';
% represents zero or more characters.
_ represents one character.
Example:
SELECT ENAME, REGEXP_INSTR(ENAME, '[A-Z]') FROM EMP;
Finds the position of the first uppercase letter in the employee
name.
4. Using REGEXP_SUBSTR
REGEXP_SUBSTR returns the substring that matches the regular
expression.
Basic Syntax:
SELECT REGEXP_SUBSTR(column_name, 'pattern') FROM
table_name;
Example:
SELECT ENAME, REGEXP_SUBSTR(ENAME, 'A[AEIOU]') FROM
EMP;
Example:
SELECT ENAME, REGEXP_REPLACE(ENAME, '[AEIOU]', '*')
FROM EMP;
Replaces all vowels in the employee's name with '*'.
Joins
1. INNER JOIN
The INNER JOIN returns only the rows where there is a
match in both tables.
Syntax:
SELECT columnsFROM table1INNER JOIN table2 ON
table1.column = table2.column; Example:
Suppose you have EMP (Employee) and DEPT (Department)
tables, and you want to find all employees with their
corresponding department details.
SELECT Emp.ENAME, Dept.DNAME FROM EMPINNER JOIN
DEPT ON Emp.DEPTNO = Dept.DEPTNO;
🔹 This query returns employees' names along with their
department names only where there is a match between
the two tables.
2. LEFT JOIN (OUTER JOIN)
The LEFT JOIN (or LEFT OUTER JOIN) returns all rows
from the left table and the matched rows from the
right table. If there is no match, NULL values will be
returned for columns from the right table.
Syntax:
SELECT columnsFROM table1LEFT JOIN table2 ON
table1.column = table2.column; Example:
Get all employees and their departments, including
employees who don't belong to any department.
SELECT E.ENAME, D.DNAME FROM EMP E LEFT JOIN
DEPT D ON E.DEPTNO = D.DEPTNO;
🔹 This query returns all employees' names. If an
employee is not assigned to a department, the
department name will be NULL.
3. RIGHT JOIN (OUTER JOIN)
The RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from
the right table and the matched rows from the left table. If
there is no match, NULL values will be returned for columns
from the left table.
Syntax:
SELECT columns FROM table1RIGHT JOIN table2 ON
table1.column = table2.column; Example:
Get all departments and their employees, including
departments without any employees.
SELECT d.DNAME
FROM DEPT d
LEFT JOIN EMP e ON d.DEPTNO = e.DEPTNO
WHERE e.EMP_ID IS NULL;
Display the total number of employees in each
department.