SQL JOIN
SQL JOIN
DDL
^^^
Create To create tables in the database.
Alter Alters the structure of the database.
Drop Delete tables from database.
Truncate Remove all records from a table, also release the space occupied by those
records
DML
^^^
Insert Inserts data into a table
Update Updates the existing data within a table.
Delete Deletes all records from a table, but not the space occupied by them.
DCL
^^^
Grant Grants permission to one or more users to perform specific tasks.
Revoke Withdraws the access permission given by the GRANT statement
TCL
^^^
Commit Saves any transaction into the database permanently.
Roll back Restores the database to last commit state.
Save point Temporarily save a transaction so that you can rollback
DQL
^^^
Select : It displays the records from the table.
===================================================================================
==========================================
DML
^^^
DELETE COMMAND
^^^^^^^^^^^^^^
The DELETE command permanently removes one or more records from the table. It
removes the entire row, not individual fields of the row, so no field argument is
needed.
For example to delete the record whose admission number is 104 the command is given
as follows:
The table will be empty now and could be destroyed using the DROP command
UPDATE COMMAND
^^^^^^^^^^^^^^
The UPDATE command updates some or all data values in a database. It can update
one or more records in a table.
The UPDATE command specifies the rows to be changed using the WHERE clause and the
new data using the SET keyword.
The above command will change the age to 20 for those students whose place is
“Bangalore”
To update multiple fields, multiple field assignment can be specified with the SET
clause
separated by comma.
For example to update multiple fields in the Student table, the command is given
as:
-----------------------------------------------------------------------------------
----------------------------
DDL
^^^
ALTER COMMAND
^^^^^^^^^^^^^
The ALTER command is used to alter the table structure like adding a column,
renaming
the existing column, change the data type of any column or size of the column or
delete the
column from the table.
ALTER TABLE <table-name> ADD <column-name><data type><size>
ALTER TABLE Student ADD Address char;
To modify existing column of table, the ALTER TABLE command can be used with MODIFY
clause like wise:
The ALTER command can be used to rename an existing column in the following way :
The ALTER command can also be used to remove a column or all columns, for example
to remove a particular column, the DROP COLUMN is used with the ALTER TABLE to
remove a particular field.
TRUNCATE command
^^^^^^^^^^^^^^^^
The TRUNCATE command is used to delete all the rows from the table, the structure
remains and the space is freed from the table.
For example to delete all the records of the student table and delete the table the
SQL statement
is given as follows:
===================================================================================
=======================
WHERE clause
^^^^^^^^^^^^
The WHERE clause is used to filter the records. It helps to extract only those
records
which satisfy a given condition. For example in the student table, to display the
list of students
of age18 and above in alphabetical order of their names, the command is given as
below:
-----------------------------------------------------------------------------------
-------
ORDER BY clause
^^^^^^^^^^^^^^^^^^
The ORDER BY clause in SQL is used to sort the data in either ascending or
descending
based on one or more columns.
-----------------------------------------------------------------------------------
-
GROUP BY clause
^^^^^^^^^^^^^^^
The GROUP BY clause is used with the SELECT statement to group the students on
rows or columns having identical values or divide the table in to groups.
HAVING clause
^^^^^^^^^^^^^
The HAVING clause can be used along with GROUP BY clause in the SELECT statement
to place condition on groups and can include aggregate functions on them.
For example to
count the number of Male and Female students having age greater than or equal to
18.
===================================================================================
====================
-----------------------------------------------------------------------------------
-----------------------------
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
SELECT MIN(Price)
FROM Products;
SELECT MAX(Price)
FROM Products;
-----------------------------------------------------------------------------------
-----------------------------
SELECT COUNT(*)
FROM Products
SELECT COUNT(ProductName)
FROM Products;
-----------------------------------------------------------------------------------
-----------------------------
SELECT SUM(Quantity)
FROM OrderDetails;
SELECT SUM(Quantity)
FROM OrderDetails
WHERE ProductId = 11
-----------------------------------------------------------------------------------
-
SELECT AVG(Price)
FROM Products;
SELECT AVG(Price)
FROM Products
WHERE CategoryID = 1;
===================================================================================
=========================
ASCII (American Standard Code for Information Interchange.)-It is used to get the
ascii code for Specified
--Character.
Select ASCII('A')
Select ASCII('S')
Select Char(97)
Select Char(115)
--Character Index: It is used to get the position of selected character.
Select CharIndex('L','saran')
Select CharIndex('O','saro')
Select Lower('jai')
Select Lower('Athletic is a Profession and Fitness is a Passion')
Select Upper('bala')
Select Upper('bala Profession is Athletic')
Select LTRIM('soundar')
Select LTRIM('soundar Passion is Fitness')
Select Reverse('kamal')
Select Reverse('Athletic')
--Left: It is used to get Left part of the Data with Specified Number of Character.
Select Left('Athletic',3)
Select Replace('Queries','e','i')
Select Len('Computer')
===========================================================================
NUMERIC FUNCTION
^^^^^^^^^^^^^^^^
POWER Returns the value of a number raised to the power of another number
SELECT POWER(4, 2);
===================================================================================
===========================
SQL Constraints
^^^^^^^^^^^^^^^
NOT NULL
^^^^^^^^
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);
UNIQUE
^^^^^^
OR
--------------------------------------------------------------------
PRIMARY KEY
^^^^^^^^^^^
CREATE TABLE Persons (
ID int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
OR
---------------------------------------------------------------------
FOREIGN KEY
^^^^^^^^^^^
CREATE TABLE Orders (
OrderID int NOT NULL PRIMARY KEY,
OrderNumber int NOT NULL,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);
OR
-------------------------------------------------------------------------------
CHECK CONSTRAINT
^^^^^^^^^^^^^^^^
OR
------------------------------------------------------------------------------
DEFAULT Constraint
^^^^^^^^^^^^^^^^^^
===================================================================================
===============================
SQL JOIN
^^^^^^^^
(INNER) JOIN: Returns records that have matching values in both tables
LEFT JOIN: Returns all records from the left table, and the matched
records from the right table
RIGHT JOIN: Returns all records from the right table, and the matched
records from the left table
FULL JOIN: Returns all records when there is a match in either left or
right table
-----------------------------------------------------------------------------------
-------
INNER JOIN
^^^^^^^^^^
THREE TABLE:
^^^^^^^^^^^
SELECT Order.Order_id, Customer.Custo_Name, Categ.Cate_Name
FROM ((Order
INNER JOIN Customer ON Order.Custo_id = Customer.Customer_id)
INNER JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID);
-----------------------------------------------------------------------------------
-----------------------------
SQL LEFT JOIN
^^^^^^^^^^^^^
-----------------------------------------------------------------------------------
-----------------------------
-----------------------------------------------------------------------------------
-----------------------------
Stored Procedure
^^^^^^^^^^^^^^^^
=> A stored procedure is a prepared SQL code that you can save,
so the code can be reused over and over again.
=> So if you have an SQL query that you write over and over again,
save it as a stored procedure, and then just call it to execute it.
EXEC pro;
-----------------------------------------------------------------------------------
-----------------------------
-----------------------------------------------------------------------------------
-----------------------------
Triggers in SQL
^^^^^^^^^^^^^^^
=> A trigger is a set of SQL statements that reside in system memory with unique
names. It is a specialized category of stored procedure
that is called automatically when a database server event occurs.
=> A trigger is called a special procedure because it cannot be called directly
like a stored procedure.
The key distinction between the trigger and procedure is that a trigger is called
automatically
when a data modification event occurs against a table.
create Trigger
^^^^^^^^^^^^^^
after insert
as
begin
from inserted
end
Alter Trigger
^^^^^^^^^^^^^
union all