0% found this document useful (0 votes)
10 views29 pages

Week 6

The document provides an overview of SQL pattern matching techniques using the LIKE operator and regular expressions with REGEXP_LIKE, REGEXP_INSTR, REGEXP_SUBSTR, and REGEXP_REPLACE. It also covers various types of SQL joins including INNER JOIN, LEFT JOIN, RIGHT JOIN, and CROSS JOIN, along with examples for retrieving employee and department data. Additionally, it discusses grouping data with GROUP BY and HAVING clauses, as well as subqueries for advanced data retrieval.

Uploaded by

ieltslearner088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views29 pages

Week 6

The document provides an overview of SQL pattern matching techniques using the LIKE operator and regular expressions with REGEXP_LIKE, REGEXP_INSTR, REGEXP_SUBSTR, and REGEXP_REPLACE. It also covers various types of SQL joins including INNER JOIN, LEFT JOIN, RIGHT JOIN, and CROSS JOIN, along with examples for retrieving employee and department data. Additionally, it discusses grouping data with GROUP BY and HAVING clauses, as well as subqueries for advanced data retrieval.

Uploaded by

ieltslearner088
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

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.

1. Find Employees Whose Name Starts with 'A'

SELECT ENAME FROM EMP WHERE ENAME LIKE 'A%';


2. Find Employees Whose Name Ends with 'N'
SELECT ENAME FROM EMP WHERE ENAME LIKE '%N';
3. Find Employees Whose Name Has 'E' as the Second Letter
SELECT ENAME FROM EMP WHERE ENAME LIKE '_E%';

4. Find Employees with 'A' in Any Position


SELECT ENAME FROM EMP WHERE ENAME LIKE '%A%';
2. Using Regular Expressions with REGEXP_LIKE
REGEXP_LIKE is used to search for a regular expression pattern within a
column.
Basic Syntax:
SELECT column1 FROM table_name WHERE REGEXP_LIKE(column_name,
'pattern');

1. Find Employees Whose Name Contains Any Digit


SELECT ENAME FROM EMP WHERE REGEXP_LIKE(ENAME, '[0-9]');

2. Find Employees Whose Name Contains Exactly 5 Characters


SELECT ENAME FROM EMP WHERE REGEXP_LIKE(ENAME, '^\w{5}$');
3. Using REGEXP_INSTR
REGEXP_INSTR returns the position of the first occurrence of a
regular expression pattern in a string.
Basic Syntax:
SELECT REGEXP_INSTR(column_name, 'pattern') FROM
table_name;

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;

Extracts the substring starting with 'A' followed by a vowel (e.g.,


'AE', 'AI') from the employee's name.
5. Using REGEXP_REPLACE
REGEXP_REPLACE is used to replace parts of a string that
match a regular expression.
Basic Syntax:
SELECT REGEXP_REPLACE(column_name, 'pattern',
'replacement') FROM table_name;

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 E.ENAME, D.DNAME FROM EMP E RIGHT JOIN DEPT D


ON E.DEPTNO = D.DEPTNO;

🔹 This query returns all department names. If a department has


no employees, the employee name will be NULL.
CROSS JOIN
The CROSS JOIN returns the Cartesian product of two
tables. This means it returns every combination of rows
from the two tables.
Syntax:
SELECT columnsFROM table1CROSS JOIN table2;
Example:
If you want to see every possible combination of
employees and departments:
SELECT E.ENAME, D.DNAME FROM EMP E CROSS JOIN
DEPT D;
🔹 This query returns every possible combination of
employees and departments, including those that don't
exist in reality.
Retrieve employee names along with their
department names.

SELECT e.ENAME, d.DNAME


FROM EMP e
INNER JOIN DEPT d ON e.DEPTNO = d.DEPTNO;
List all employees along with their department
names, including those who are not assigned to
any department.

SELECT e.ENAME, e.DEPTNO, d.DNAME


FROM EMP e
LEFT JOIN DEPT d ON e.DEPTNO = d.DEPTNO;
Display all department names along with
employees, including departments that have no
employees.

SELECT d.DNAME, e.ENAME


FROM EMP e
RIGHT JOIN DEPT d ON e.DEPTNO = d.DEPTNO;
Find the names of employees along with their
managers’ names.

SELECT e1.ENAME AS Employee, e2.ENAME AS


Manager
FROM EMP e1
LEFT JOIN EMP e2 ON e1.MGR = e2.EMP_ID;
List all possible combinations of employees and
departments.

SELECT e.ENAME, d.DNAME


FROM EMP e
CROSS JOIN DEPT d;
Retrieve employees who work in the "SALES"
department.

SELECT e.ENAME, e.JOB, d.DNAME


FROM EMP e
INNER JOIN DEPT d ON e.DEPTNO = d.DEPTNO
WHERE d.DNAME = 'SALES';
Find departments that do not have 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.

SELECT d.DNAME, COUNT(e.EMP_ID) AS


Total_Employees
FROM DEPT d
LEFT JOIN EMP e ON d.DEPTNO = e.DEPTNO
GROUP BY d.DNAME;
Find employees who earn more than their
department's average salary.

SELECT e.ENAME, e.SAL, d.DNAME


FROM EMP e
JOIN DEPT d ON e.DEPTNO = d.DEPTNO
WHERE e.SAL > (SELECT AVG(SAL) FROM EMP
WHERE DEPTNO = e.DEPTNO);
Group by clause
SELECT column_name,
aggregate_function(column_name) FROM
table_name GROUP BY column_name;
Find the total salary paid in each department.

SELECT DEPTNO, SUM(SAL) AS Total_Salary


FROM EMP
GROUP BY DEPTNO;

Find the total salary paid for each job in each


department.

SELECT DEPTNO, JOB, SUM(SAL) AS Total_Salary


FROM EMP GROUP BY DEPTNO, JOB;
HAVING Clause (Filtering Groups)
Find departments where total salary exceeds
50000.

SELECT DEPTNO, SUM(SAL) AS Total_Salary


FROM EMP
GROUP BY DEPTNO
HAVING SUM(SAL) > 50000;
Find the number of employees in each
department.

SELECT DEPTNO, COUNT(*) AS Employee_Count


FROM EMP
GROUP BY DEPTNO;

Find departments where the average salary is


more than 20000.
SELECT DEPTNO, AVG(SAL) AS Avg_Salary
FROM EMP
GROUP BY DEPTNO
HAVING AVG(SAL) > 20000;
Find the highest and lowest salaries in each
department.

SELECT DEPTNO, MAX(SAL) AS Max_Salary,


MIN(SAL) AS Min_Salary
FROM EMP
GROUP BY DEPTNO;
Retrieve department numbers and employee counts for
departments with more than 5 employees earning above
10,000.

SELECT DEPTNO, COUNT(*) AS Employee_Count FROM EMP


WHERE SAL > 10000 -- Filters before grouping
GROUP BY DEPTNO
HAVING COUNT(*) > 5;
subqueries
Find employees who earn more than the
average salary.

SELECT ENAME, SAL


FROM EMP
WHERE SAL > (SELECT AVG(SAL) FROM EMP);
Find employees who work in departments with
more than 5 employees.

SELECT ENAME, DEPTNO


FROM EMP
WHERE DEPTNO IN
(SELECT DEPTNO FROM EMP GROUP BY
DEPTNO HAVING COUNT(*) > 5);
SELECT
Find employees who earn above the average
salary of their department.

SELECT ENAME, SAL, DEPTNO


FROM EMP e1
WHERE SAL > (SELECT AVG(SAL) FROM EMP e2
WHERE e1.DEPTNO = e2.DEPTNO);
Show the top 3 highest-paid employees.

SELECT ENAME, SAL


FROM (SELECT ENAME, SAL FROM EMP ORDER
BY SAL DESC)
WHERE ROWNUM <= 3;

You might also like