Monday, August 8, 2016

SQL Select Top Clause



The SQL select top clause is used to return the number of records. It is very helpful when we want to fetch the record from a very large table that contains thousands of records.

Example –
Firstly we create a table with name “emp”-

 Select top 2 * from emp

Output - 



SQL Select TOP PERCENT Example -

Select top 50 percent * from emp
Output -



Friday, August 5, 2016

SQL AND & OR operators



The SQL AND & OR operators used to filter the record that is based on one or more condition.
The AND operator used to show the record if both first and second condition are true.
The OR operator used to show the record if either the first condition OR second condition is true.
Example – Firstly we create the table “emp

 
AND operator Example –

select * from emp where id='1' AND name='sachin'

Output – 

OR operator Example –

select * from emp where id='1' OR name='pramod'

Output – 
                                                               Author - Sachin Pathak


SQL Foreign key Constraint



A Foreign key in one table points to a primary key in another table.
Firstly we create two tables:-
employee” table: -

E_Id
Name
Address
1
Sachin
Kanpur
2
Amit
Aligarh
3
Sumt
Etah

Emp” table
Emp_id
dept
E_Id
1
IT
2
2
sales
1

The SQL Foreign key constraint on create table
Create table Emp
(
Emp_id int NOT NULL PRIMARY KEY,
dept varchar (250) NOT NULL,
E_Id int Foreign Key references employee(E_Id)
)

SQL foreign Key constraint on ALTER Table
ALTER Table Emp
Add foreign key (E_Id)
References Employee (E_Id)

Drop a Foreign Key Constraint –
Alter table Emp
Drop contraint E_Id

                                                Author : - Sachin Pathak