SQL is a standard language used to create, access, and manipulate databases. There are 4 types of SQL languages: data definition language, data manipulation language, transaction control language, and data control language. The data definition language is used to define database schemas and the data manipulation language contains commands like SELECT, INSERT, UPDATE, and DELETE to query and manipulate data.
1. SQL (Structured Query Language)
SQL Queries:
• 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
2. CREAT A TABLE:
CREAT TABLE cricket (sarov varchar (20), six int, four int, two int, out int)
• ALTER TABLE - alters a database table
ADD COLUMN:
ALTER TABLE cricket ADD hundreds int
DROP COLUMN:
ALTER TABLE cricket DROP column hundreds
• DROP TABLE - deletes a database table
DROP cricket
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
3. • 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’)
4. 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:
Joins are to retrive data from two or more tables.
JOIN Refering of two tables:
Select table1.anycolumn,table2.anycolumn from table1,table2 where
table1.samecolumn=table2.samecolumn
select sarov.descripition,janika.company from sarov,janika where
sarov.descripition=janika.descripition
5. Inner joint
The intersection values of the two application.
Select field1.column1, field2.column2 from table1 INNER JOIN table2 ON
table1.samevalue=table2.samevalue.
Select oviya.serial, oviya1.Mgf from oviya inner join oviya1 on
oviya.company=oviya1.company
Left join:
Left join means display all the rows in table1 even there is no match in
table2.
select oviya.serial,oviya1.Mgf from oviya left join oviya1 on
oviya.company=oviya1.company
Right join:
Right join means display all the rows in table2 even there is no match in
table1.
select oviya.serial,oviya1.mgf from oviya right join oviya1 on
oviya.company=oviya1.company
Union :
(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
data.
Select company from oviya UNION select company from oviya1
6. Union ALL:
(display the same name twice)Union All is used to display all the same
column name
select company from oviya UNION ALL select company from oviya1
Order by:
Order by command is used to display in alphabetic order .
select company,model from oviya ORDER by company
Group by:
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
Having:
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.
Where:
It is used to select particular value, if it is conditional.
select oviya.serial,oviya1.Num from oviya,oviya1 where
oviya.company=oviya1.company
ALIAS:
7. 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
Between(AND):
It is used to select inbetween data’s only.
Select*from table1 where column1 between ’first name’ and ‘Last name’
Between (NOT):
Select*from table1 where coolumn1 NOT Between ‘first name’ and ‘last name’
AND:
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'
OR :
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’
8. 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'
OR
Select* from food where mobile='1100' or mobile='2200' and country='India'
IN:
To find the particular two details in the row is called IN
Select*from oviya2 where position in('test','j test')
Select *from table1 where column1 in (firstname, lastname)
Function:
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.
9. STRUCTURE:
• Structure can not return value
• We can not pass any argument to the structure.
DIFFERENCE B/W HAVING & WHERE:
HAVING WHERE
HAVING is used only in Aggregate function Where is not used in the Aggregate function
SELECT INTO:
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