SQL Notes
SQL Notes
(DDL)
To Create table
To delete column
Rename column
To ensure that the data is updated correctly so that we can roll back.
BEGIN trans
UPDATE NXU_Employee
SET phone_no = '9078942415'
Commit Trans
UPDATE NXU_Employees
SET phone_no = '9971521402', email = 'anupamasingh@gmail.com'
where emp_id = 2
Update NXU_Employees
SET phone_no = Null, email = Null
Where emp_id in (3,4,5)
The select top clause is used to specify the number of records to returns
Aggregate functions
MIN()
- It returns the smallest values within the selected column
MAX()
- It returns the largest values within the selected column
COUNT()
AVG()
UPDATE Products
Set product_name = ' Chef Anton''s Gumbo Mix' WHERE product_id = 5
Alias
SQL Joins
Inner Joins
LEFT JOIN: Returns all the records from the left table and matching records from the right table.
RIGHT Joins
RIGHT JOIN: Returns all the records from the right table and matching records from the left table.
RIGHT Outer JOIN: Returns all the records from the right table and excludes matching records from
the left table.
Full JOIN: Returns all the records from the left table and the right table.
Full Join
FULL Outer Join
Full Outer JOIN: Returns all the records from the left table and the right table and excludes the
matching records from both the tables.
Select Into statement - To select data from one table and insert into new table.
SQL Constraints
Foreign Key
Auto increment
View
Exists Operator – This is used to test for the existence of any record in sub querry.
Else
Begin
Select 2
End
All Operator – It returns true if all of the sub query value meets the conditions.
Not = operator ! =
Not Like
Not Between
Between
Select * from Customer where customer_id BETWEEN 30 and 40
Comments
M SQL Operator
Arithmetic operators
Add (+)
Subtract (-)
Multiply (*)
Divide (/)
Modulo (%)
Bitwise Operators
And (&)
OR (|)
Bitwise exclusive or (^)
Comparison Operators
Equal to (=)
Greater than (>)
Less than (<)
Greater than or Equal to (>=)
Less than or Equal to (<=)
Not Equal to (!=) (<>)
Group by
The statement groups rows that have the same value into summary rows.
This is often used with the aggregate (Count(), min(), max(), SUM(), Avg() function.)
This goes through conditions and returns a value when the first condition is met.
Null Function
Having
Having clause was added to SQL because the where keyword cannot be used with aggregate function.
Union
Store Procedures