SQL New Notes
SQL New Notes
STEP 1
STEP 2
STEP 3
STEP 4
STEP 5
STEP 6
STEP 7
STEP 8
STEP 9
Install SSMS
STEP 10
Click -> Free download for SQL Server management studio (SSMS)
File Downloading…
STEP 11
After Downloading
STEP 12
Click Install
STEP 13
Close
STEP 14
Installation Completed…
What is SQL?
SQL stands for Structured Query Language
SQL lets you access and manipulate databases
SQL became a standard of the American National Standards Institute
(ANSI) in 1986, and of the International Organization for Standardization
(ISO) in 1987
RDBMS is the basis for SQL, and for all modern database systems such as MS
SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.
We will create a new database, if you want to use a specific database, use this
query.
use EmployeeDetail
4. Insert table:
Used to insert the field details.
Example:
insert into EmployeeRecord values (1,'Aswin',1000,'Trichy',9876432108)
insert into EmployeeRecord values (2,'Aaron',2000,'Chennai',9597804309)
insert into EmployeeRecord values (3,'James',3000,'Dubai',8907654039)
insert into EmployeeRecord values (4,'Jhon',4000,'Madhurai',9789651234)
insert into EmployeeRecord values (5,'Praveen',5000,'Chennai',7865439201)
Screenshot for Example:
5. Select table:
If you want see the table columns use this select statement.All coulmns
are selected.
Example:
select * from EmployeeRecord
Def:ID,Name ... are the field names of the table you want to select data
from.
Select ID,Name from EmployeeRecord
Example:
SELECT DISTINCT Address FROM EmployeeRecord
Example:
select * from EmployeeRecord where name like '%a%'
6. Update table:
If you want to change the field values use update statement. Change one or
more fields.
Example:
i)
update employeerecord set name='Raja' where name='Praveen'
ii)
update employeerecord set name='Raja', Salary=6000 where name='Madhan'
7. Delete table:
It is used to delete the all field values and table.
Example:
delete from EmployeeRecord
iii) TRUNCATE TABLE: is used to delete the data inside a table, but not the
table itself.
Example:
TRUNCATE TABLE EmployeeRecord
8. Comments:
Comments are used to explain sections of SQL statements, or to prevent
execution of SQL statements.
Singleline Comment:
Example:
--select * from Student
Multiline Comments:
Example:
/*Select * from StaffDetail
Select * from Stueden
Select * from EmployeeRecord*/
Select * from test1
9. SQL Fuctions:
i) MIN(): It returns the smallest value in the table.
Example:
select min(id) from employeerecord
iii) COUNT(): It returns the number of rows that matches a specified criterion.
Example:
select count(address) from EmployeeRecord
11. Operators:
I) Logical Operators:
i) IN & NOT IN Operators:
IN():It allows to specify multiple values in a WHERE clause. And shorthand
for multiple OR conditions.
Example:
select * from employeerecord where Address in ('Chennai','Dubai')
NOT IN():It return all records that are NOT any of the values in the list.
Example:
select * from employeerecord where Address not in ('Chennai','Dubai')
NOT BETWEEN: Used to select values within a given range. The values can
be numbers, text, or dates.
Example:
select * from EmployeeRecord where Salary not between 1000 and 3000
Screenshot
SQL ANY and ALL
SQL ANY Operator
SQL ANY compares a value of the first table with all values of the second
table and returns the row if there is a match with any value.
It has the following syntax:
SELECT column
FROM table1
WHERE column OPERATOR ANY (
SELECT column
FROM table2
);
Here,
Note: The column placeholder can stand for multiple different columns from
the two tables.
SELECT *
FROM Teachers
WHERE age = ANY (
SELECT age
FROM Students
);
Here, the subquery below returns all the ages from the Students table.
SELECT age
FROM Students
SELECT *
FROM Teachers
WHERE age < ANY (
SELECT age
FROM Students
);
Here, the SQL command selects rows if age in the outer query is less than
any age in a subquery.
Example: ANY in SQL
SQL ALL Operator
SQL ALL compares a value of the first table with all values of the second
table and returns the row if there is a match with all values.
SELECT column
FROM table1
WHERE column OPERATOR ALL (
SELECT column
FROM table2
);
Here,
Note: The column placeholder can stand for multiple different columns from
the two tables.
SELECT *
FROM Teachers
WHERE age > ALL (
SELECT age
FROM Students
);
Here, the subquery below returns all the ages from the Students table.
SELECT age
FROM Students
If the teacher's age is greater than all student's ages, the corresponding
row of the Teachers table is selected.
Example: ALL in SQL
SQL – AGGREGATE FUNCTIONS
Group by
Eg 1
o/p
Eg 2
o/p
Order by
Having
Eg . Table
Eg 1
o/p
SQL JOINS
A JOIN clause is used to combine rows from two or more
tables, based on a related column between them.
A self join is a regular join, but the table is joined with itself.
(OR)
IN NORMAL TABLE WE CAN ALTER THE
PRIMARY KEY CONSTRAINT
In this table compulsory mention the NOT NULL in
StudentID Column, otherwise we get the error
COMPOSITE KEY
How to more than one column in primary key?
SQL NOT NULL Constraint
SQL UNIQUE Constraint
SQL CHECK Constraint
SQL DEFAULT KEY Constraint
SQL FOREIGN KEY Constraint
The FOREIGN KEY constraint is used to prevent actions that would destroy links between
tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to
the PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with the primary key is
called the referenced or parent table.
or
//ERROR
insert into Students values(1002,'Aishwarya','Jayaram','2005-09-20','F',405)