SQL Joins
SQL Joins
The join clause allows us to retrieve data from two or more related tables
Types of Joins:
1.Inner join
Inner Join Returns the rows(records) that have matching data in both tables
2.Left join
Left join Returns all records from the left table, and the matched records
from the right table
3.Right join
Right join Returns all records from the right table, and the matched records
from the left table
4.Full join
Full join Returns all records from both tables where there is a match in either
left or right table. If there is no match it will show null values(blank)
5.Cross Join:
The cross join returns all records from both tables (table1 and table2) whether
the other table matches or not.
Cross join produces a Cartesian product of the two tables.
Syntax:
SELECT column_name(s) FROM table1 CROSS JOIN table2;
Ex:
Select * from mobiles cross join accessories;
6.Self Join:
self join is a type of join where a table is joined with itself. This allows to
compare rows within the same table.
Syntax:
SELECT column_name(s) FROM table_name aliasname1
JOIN table_name aliasname2 ON aliasname1.column_name =
aliasname2.column_name;
Ex:
select * from mobiles t1 join mobiles t2 on t1.mid=t2.mid;
Index:
Index is mainly used to speed up the searching of data in the database table.
Syntax:
Example:
Create index myindex on mobiles(sid);
View:
Views can be used as security mechanism by letting users access the table data
without granting the permission to the tables
Syntax:
create view viewname as select column names from tablename;
Example:
create view myview as select id,name,salary,bonus,tax,department from
employees;