Skip to main content

Inner join in sql server

INNER JOIN

Inner join responsible to returns rows which have a match in both tables.
See following example:-
CREATE TABLE #ORDER
(
orderId INT IDENTITY(1,1),
fName VARCHAR(400),
lName VARCHAR(400),
ShipAddress VARCHAR(1000),
dtCreate DATETIME
)

CREATE TABLE #ORDER_DETAILS
(
orderDetailsId INT IDENTITY(1,1),
productName VARCHAR(400),
pPrice DECIMAL(18,2),
iQTY INT,
orderId INT,
dtCreate DATETIME
)

INSERT INTO #ORDER(fName,lName,ShipAddress,dtCreate)
SELECT 'Dilip','SINGH','New Delhi',GETDATE()
UNION ALL
SELECT 'Nirmit','katiyar','Mumbai',GETDATE()
UNION ALL
SELECT 'Raj','Dongra','Jharkhand',GETDATE()
UNION ALL
SELECT 'Salim','safi','Muradnagar UP',GETDATE()


INSERT INTO #ORDER_DETAILS(productName,pPrice,iQTY,orderId,dtCreate)
SELECT 'Product 1', 180,3,1,GETDATE()
UNION ALL
SELECT 'Product 2', 120,1,2,GETDATE()
UNION ALL
SELECT 'Product 3', 100,1,3,GETDATE()
UNION ALL
SELECT 'Product 4', 40,2,4,GETDATE()
UNION ALL
SELECT 'Product 5', 90,1,5,GETDATE()
--INNER JOIN
SELECT ORD.fName,ORD.lName,ORD.ShipAddress,ORDTL.productName, ORDTL.iQTY,ORDTL.pPrice,ORD.dtCreate FROM #ORDER AS ORD
INNER JOIN #ORDER_DETAILS AS ORDTL ON
ORDTL.orderId=ORD.orderId


DROP TABLE #ORDER

DROP TABLE #ORDER_DETAILS


Result;
Here you can see 5 rows from #ORDER_DETAILS table is not showing

Comments

Popular posts from this blog

Constraints in sql server

Constraints   - Constraints are the rules enforced on data columns on table. This ensures the accuracy and reliability of the data in the database. Constraints define some conditions that restricts the column to remain true while inserting or updating or deleting data in column. Constraints can be defined in two ways   1) The constraints can be specified immediately after the column definition. This is called column-level definition.   2) The constraints can be specified after all the columns are defined. This is called table-level definition.   Types of Constraints in SQL-Server NOT NULL - This constraint is responsible for a column to confirm that a column cannot have a null value. DEFAULT – This constraint provides a default value when specified non for this column. UNIQUE - This constraint insure that each row for the column has different value. PRIMARY KEY - Primary key constraint is a combination of a NOT NULL constraint and a UNIQUE constra

Bulk Update in sql server 2008..

/*Bulk update query*/ create table Table1 ( ID int identity ( 1 , 1 ), vIsbn varchar ( 20 ), dcPrice decimal ( 18 , 2 ) ) create table Table2 ( ID int identity ( 1 , 1 ), vIsbn varchar ( 20 ), dcPrice decimal ( 18 , 2 ) ) /*Here create two tables, we update Table1's dcPrice column from Table 2 Using Bulk update*/     insert into Table1 ( vIsbn , dcPrice ) values ( '9087657654312' , 0.00 ) insert into Table1 ( vIsbn , dcPrice ) values ( '9989876765454' , 0.00 ) insert into Table2 ( vIsbn , dcPrice ) values ( '9087657654312' , 10.20 ) insert into Table2 ( vIsbn , dcPrice ) values ( '9989876765454' , 20.10 ) select * from Table1 /*here bulk update Query*/ UPDATE Tbl SET dcPrice = Tbl2 . dcPrice FROM Table1 Tbl JOIN Table2 AS Tbl2 ON Tbl . vIsbn = Tbl2 . vIsbn select * from Table1 drop table Table1 dr

DEFAULT constraint in sql server

DEFAULT – This constraint provides a default value when specified non for this column. Example- Following create a table PRODUCT, column DTCREATE has default value GETDATE() it will store system’s date-time by default if value is not specified for this column. CREATE TABLE PRODUCT ( ID INT NOT NULL, SKU VARCHAR ( 20 ) NOT NULL, TITLE VARCHAR ( 200 ) NOT NULL, PRICE MONEY NOT NULL, DISCRIPTION VARCHAR ( 2000 )   NULL, DTCREATE DATETIME DEFAULT GETDATE () ) If PRODUCT table has already been created and you want apply default price in PRICE column use following query (for SQL server). ALTER TABLE PRODUCT ADD CONSTRAINT DF_SomePrice DEFAULT 20.00 FOR PRICE