SQL (Structured Query Language)
• SQL is a standard computer language for accessing and manipulating database.
• SQL statement are used to retrieve and update data in a database.
Types of Language;
There are 4 types of language
1. Data Definition Language.
2. Data Manipulation Language.
3. Transaction Control Language.
4. Data Control Language.
1. DATA DEFINITION LANGUAGE:
The Data Definition Language (DDL) permits database
Tables to be created or deleted.
CREATE DATABASE- creates a new database table
CREAT DATABASE Cricket
CREAT A TABLE:
CREAT TABLE cricket (sarov varchar (20), six int, four int, two int, out int)
ALTER TABLE - alters a database table
ALTER TABLE cricket ADD hundreds int
ALTER TABLE cricket DROP column hundreds
DROP TABLE - deletes a database table
2. Data Manipulation Language:
DML is used for executing queries.
SELECT - extracts data from a database table
To select any two columns:
SELECT India, gold FROM sundar
To select all columns:
SELECT * FROM sundar
UPDATE – It is used to modify the data in a table.
UPDATE Sundar SET name=’north’ where India=’ravi’
4Update one column in a row:
UPDATE sundar SET name =’sarov’ WHERE name=’priya’
Update several Columns in a row:
UPDATE sundar SET gold=’55’, name = ‘99’ WHERE name=’priya’
DELETE - deletes data from a database table.
DELETE FROM sundar Where India=’abi’
Delete a Row:
DELETE FROM sundar where name=’rajesh’
Delete all Row:
DELETE FROM sundar
INSERT INTO - inserts new data into a database table
INSET INTO sundar values (‘rajesh’,’50’,’55’,’60’,’6’)
INSERT DATA IN A SPECIFIED COLUMN:
INSERT INTO sundar (name, four) values (‘priya’, ‘20’)
To select the maximum value:
select max(two)from cricket
To Select the 2nd maximum Value:
select max(two)from cricket where two<(select max(two) from cricket)
To Select the 5th maximum value:
select max(six)from cricket as x where 5=(select count(distinct
six)from cricket where six>=x.six)
Select max(column2) FROM table1 AS X where N=(SELECT COUNT (DISTINCT
Column2) FROM table1 where column2>=X.column2)
To select the minimum value:
select min (six)from cricket
To Select the 2nd minimum value:
select min(six)from cricket where SIX >(select min(six)from cricket)
Joins are to retrive data from two or more tables.
JOIN Refering of two tables:
Select table1.anycolumn,table2.anycolumn from table1,table2 where
select sarov.descripition,janika.company from sarov,janika where
The intersection values of the two application.
Select field1.column1, field2.column2 from table1 INNER JOIN table2 ON
Select oviya.serial, oviya1.Mgf from oviya inner join oviya1 on
Left join means display all the rows in table1 even there is no match in
select oviya.serial,oviya1.Mgf from oviya left join oviya1 on
Right join means display all the rows in table2 even there is no match in
select oviya.serial,oviya1.mgf from oviya right join oviya1 on
(display the same name in one time)Union command is used to display
common columns in table1 & table2,when using union all column should be same
Select company from oviya UNION select company from oviya1
(display the same name twice)Union All is used to display all the same
select company from oviya UNION ALL select company from oviya1
Order by command is used to display in alphabetic order .
select company,model from oviya ORDER by company
To find the sum of each and every rows(if two row’s have same name but
the amount is varies it addes that two amount values in one name and)
Select column1,sum(column2)from table1 group by column2
select company,sum(sum)from oviya2 group by sum
If the column is aggregate function we use having.
(i.e. we can also use where if it is not aggregate function)
Select column1, sum (summing column) from table1 group by column1
having Sum (summing column) condition value.
It is used to select particular value, if it is conditional.
select oviya.serial,oviya1.Num from oviya,oviya1 where
Changing the column name or table name
Column name alias:
Which is used to change the column name only
Select column1 as newcolumn1, column2 as newcolumn2 from table1
Table name alias:
Which is used to change the table name only
Select column1, column2 from table1 AS newtable1
It is used to select inbetween data’s only.
Select*from table1 where column1 between ’first name’ and ‘Last name’
Select*from table1 where coolumn1 NOT Between ‘first name’ and ‘last name’
It is used to display a row even if we give any two columns with particular row
select*from cool where SI='3'and Price='14'
It is used to display both the items with same name,it
is enough to give one command.
select*from cool where Items='mirinda' or Price='10'
SELECT *FROM table1 where column1=’value’ or column2=’value’
AND & OR:
Joins two or more conditions in a WHERE clause
Select column1,column2,column3 FROM table WHERE column1=value1 AND
column3= value2 OR column3= value2
Select country, mobile, status from food where country='India' and
Status='good' or status='bad'
Select* from food where mobile='1100' or mobile='2200' and country='India'
To find the particular two detai s in the row is called IN
Select*from oviya2 where position in('test','j test')
Select *from table1 where column1 in (firstname, lastname)
It is the aggregate function for calculation and counting.
* Function can return value
* We can pass arguments to the function
Select function (column) from table.
Two types of Function
1. AGGREGATE FUNCTION:
Operate against the collection of values but returns single value.
2. SCALAR FUNCTION:
Operate against the collection of single values but returns single value.
Structure can not return value
We can not pass any argument to the structure.
DIFFERENCE B/W HAVING & WHERE:
HAVING is used only in Aggregate function Where is not used in the Aggregate functi n
It is used to take backup from the previous table
select SI,Items,Price,Quantity into oviya5 from cool
Select column1, column2, column3 into new table from table1
TO DISPLAY ALL THE COLUMN ONCE MORE
Select column1, column2, column3, table1.* from table1