Skip to content

Commit

Permalink
SQL PHP Cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
astechedu committed Jan 11, 2024
1 parent fd3fc5e commit e9065a1
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 203 deletions.
161 changes: 160 additions & 1 deletion sqlphp/db.notes/sql.join.queries.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ Video: Natural join details with pictures in hindi
https://youtu.be/zVZzJHSQlSw?t=289


//Self join using inner, left joins
https://dotnettutorials.net/lesson/self-join-mysql/


//Full Join Examples
https://mysqlcode.com/mysql-full-join/

----------------------------------------------------------------------------------

Expand Down Expand Up @@ -65,7 +71,7 @@ Example:



3. Self Join
3. Self Join <---

//Self Join is a type of inner join, which is performed in cases where the comparison between
//two columns of a same table is required;
Expand Down Expand Up @@ -108,10 +114,163 @@ WHERE a.SALARY < b.SALARY
ORDER BY a.SALARY;



SELECT E.FullName as Employee, M.FullName as Manager
FROM Employee E
LEFT JOIN Employee M
ON E.ManagerId = M.EmployeeId;


--> Inner Self Join Example:

SELECT E.FullName as Employee, M.FullName as Manager
FROM Employee E
INNER JOIN Employee M
ON E.ManagerId = M.EmployeeId;


--> Cross Self Join Example in MySQL::

SELECT E.FullName as Employee, M.FullName as Manager
FROM Employee E
CROSS JOIN Employee M;




-----x-----




---> FULL JOIN <---


The FULL JOIN or FULL OUTER JOIN keyword is used to select all records from the left table and right table.


1.

SELECT *
FROM table1 t1
FULL OUTER JOIN table2 t2
ON t1.id = t2.id


Not Work in Mysql:



2.

SELECT *
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id

UNION ALL

SELECT *
FROM table1 t1
RIGHT JOIN table2 t2
ON t1.id = t2.id
WHERE t1.id IS NULL


3.

With two tables t1, t2:

SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id


4.

SELECT *
FROM Students s
LEFT JOIN Marks m ON s.Id = m.StudentID
UNION
SELECT *
FROM Students s
RIGHT JOIN Marks m ON s.Id = m.StudentID
WHERE s.Id IS NULL;




5.
Swapping Table Sides //Changing tables positions right to left or left to right

SELECT *
FROM Marks m
LEFT JOIN Students s
ON m.StudentID = s.Id
UNION
SELECT *
FROM Marks m
RIGHT JOIN Students s
ON m.StudentID = s.Id
WHERE m.StudentID IS NULL;


6. FULL JOIN With the WHERE clause


SELECT *
FROM Students s
LEFT JOIN Marks m ON s.Id = m.StudentID
WHERE s.Id > 5

UNION

SELECT *
FROM Students s
RIGHT JOIN Marks m ON s.Id = m.StudentID
WHERE s.Id > 5 AND s.Id IS NULL;


7.

SELECT *
FROM Students s
LEFT JOIN Marks m
ON s.Id = m.StudentID
WHERE m.StudentID > 5

UNION ALL

SELECT *
FROM Students s
RIGHT JOIN Marks m
ON s.Id = m.StudentID
WHERE s.Id IS NULL AND m.StudentID > 5;


8.
FULL JOIN in MariaDB:

SELECT * FROM Students s LEFT OUTER JOIN Marks m ON s.ID=m.StudentID
UNION
SELECT * FROM Students s RIGHT OUTER JOIN Marks m ON s.ID=m.StudentID;




---x----










----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

Expand Down
202 changes: 0 additions & 202 deletions sqlphp/db.notes/sql.joins.txt

This file was deleted.

0 comments on commit e9065a1

Please sign in to comment.