Skip to main content

Group By and Having clause

 Group By and Having The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each dept". The GROUP BY statement is must be used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns. GROUP BY Syntax

SQL JOIN | SQL JOINS | SQL JOINS with Examples | SQL JOIN TABLES | SQL JOINS TUTORIAL

 SQL JOINS

SQL Joins basically used for mapping the data from more than 1  tables.

For correct out output we need to map correct respective columns.



Following example will demonstrate how to use sql joins

SQL Joins


TYPES of SQL JOINS:

followings are joins type in sql

1.Inner Join
2.Left Join 
3.RIght Join
4.Full outer Join

Table setup for Join example.

Create Table CityMaster_k4Coding
(
id bigint identity(1,1),
CityName varchar(100)
)

Create Table StudentMaster_k4Coding
(
 STudentName Varchar(100),
 Standard varchar(100),
 Admissiondate datetime,
Cityid bigint
)

Insert Into cityMaster_k4coding values ('MUMBAI') -- auto id 1
Insert Into cityMaster_k4coding  values ('DELHI') -- auto id 2
Insert Into cityMaster_k4coding  values ('PUNE') -- auto id 3
Insert Into cityMaster_k4coding  values ('CHENNAI') -- auto id 4


Insert Into  StudentMaster_k4Coding  values ('ASHISH','X',getdate(),1)
Insert Into  StudentMaster_k4Coding  values ('Jay','X',getdate(),2)
Insert Into  StudentMaster_k4Coding  values ('VIJ','X',getdate(),3)
Insert Into  StudentMaster_k4Coding  values ('PAREKH','X',getdate(),4)
Insert Into  StudentMaster_k4Coding  values ('David','X',getdate(),1)
Insert Into  StudentMaster_k4Coding  values ('Rohan','X',getdate(),2)
Insert Into  StudentMaster_k4Coding  values ('Swap','X',getdate(),2)
Insert Into  StudentMaster_k4Coding  values ('Mutthu','X',getdate(),5)

Tutorial Video


City Master :Sample data

Query :Select * from cityMaster_k4coding

SQL JOINS



Student Master:  Sample data

Query : Select * from StudentMaster_k4Coding

SQL JOINS


Inner Join :

Inner Join is used when matched records need to be extracted.



As per sample data in City we have respective name and  ids as 1.2.3 & 4 where as in Student table we have address ids 1,2,3,4,5.

We we apply inner join query based on city id unique field in this case only matched records will display.

Query Syntax:
Select * from tablename1 S
Inner Join tablename1  C on
S.uniquefield=c.uniquefield 

Note: Here S and C are the table alias and uniquefield is common fields in both table for join.

Select * from StudentMaster_k4Coding S
Inner Join cityMaster_k4coding C on
S.Cityid=c.id


System showing the out put where matching records are available .

In City master table there is no ID=5 so query is not displaying that additional record.

SQL JOINS


Left Join :

Left Join is used when all matched records need to be extracted from left side of the table data.

Similar to Inner Join query 

Select * from StudentMaster_k4Coding S
Left Join cityMaster_k4coding C on
S.Cityid=c.id

here we have StudentMaster_k4Coding  S as left side table, so all the output will display from student table.




As per above screen no data present in City table and we have left join from student table because of that its showing cityid and name as NULL.

RIGHT Join :

Left Join is used when all matched records need to be extracted from left side of the table data.

Similar to LEFT Join query we are doing join with RIGHT keyword

Select * from cityMaster_k4coding C 
RIGHT Join StudentMaster_k4Coding S on
S.Cityid=c.id

here we have StudentMaster_k4Coding  S as left side table, so all the output will display from student table.

SQL JOINS



FULL Outer Join :

FULL outer Join is used when all matched records need to be extracted from both side of the table data.

Similar to RIGHT Join query we are doing join with FULL OUTER JOIN keyword

Select * from cityMaster_k4coding C 
FULL OUTER Join StudentMaster_k4Coding S on
S.Cityid=c.id

here we have StudentMaster_k4Coding  S as left side table, so all the output will display from student table and City table both.

SQL JOINS

Comments

Popular posts from this blog

How to convert null value to decimal in SQL Server?

 How to convert null value to decimal in SQL Server? While writing the sql queries s most issue coming when no value available in table and some calculation needs to perform. in following scenario salary value is null but it needs to display in same format like wise other value appearing in table for salary. First we need to handle the null value. select empid,empname,dept, isnull(salary,0) as salary from k4coding_emp if require further conversion then use following command to convert the value. select empid,empname,dept,  cast(isnull(salary,0) as decimal(18,2)) as salary from k4coding_emp

SQL DML, DML and DQL commands

 SQL DML, DML and DQL commands 1. DDL(Data Definition Language) : DDL or Data Definition Language really comprises of the SQL orders that can be utilized to characterize the information base diagram. It just arrangements with portrayals of the information base pattern and is utilized to make and adjust the structure of data set articles in the data set.  Instances of DDL orders:  ○ CREATE –is utilized to make the information base or its articles (like table, file, work, sees, store system and triggers).  ○ DROP –is utilized to erase objects from the information base.  ○ ALTER-is utilized to adjust the structure of the information base.  ○ TRUNCATE–is utilized to eliminate all records from a table, including all spaces dispensed for the records are taken out.  ○ COMMENT –is utilized to add remarks to the information word reference.  ○ RENAME –is utilized to rename an item existing in the information base.  2. DQL (Data Query Language) :  ...