SQL (Structured Query Language)
SQL (Structured Query Language)
SQL (Structured Query Language)
What is SQL?
SQL is Structured Query Language, which is a computer language for storing,
manipulating and retrieving data stored in a relational database.
SQL is the standard language for Relational Database System. All the
Relational Database Management Systems (RDMS) like MySQL, MS Access,
Oracle, Sybase, Informix, Postgres and SQL Server use SQL as their standard
database language.
Why SQL?
SQL is widely popular because it offers the following advantages −
Allows users to define the data in a database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries &
pre-compilers.
Basic Structure
A relational database is a collection of tables. Each table has its own unique
name.
There are five types of SQL commands: DDL, DML, DCL, TCL, and
DQL.
1. Data Definition Language (DDL)
This includes changes to the structure of the table like creation of table, altering
table, deleting a table etc.
All DDL commands are auto-committed. That means it saves all the changes
permanently in the database.
Command Description
create to create new table or database
alter for alteration
truncate delete data from table
drop to drop a table
rename to rename a table
CREATE TABLE
The SQL CREATE TABLE statement is used to create a new table.
Syntax
ALTER TABLE
The SQL ALTER TABLE command is used to add, delete or modify columns
in an existing table. You should also use the ALTER TABLE command to add
and drop various constraints on an existing table.
Syntax
The basic syntax of an ALTER TABLE command to change the DATA TYPE
of a column in a table is as follows.
ALTER TABLE table_name MODIFY COLUMN column_name datatype;
TRUNCATE TABLE
The SQL TRUNCATE TABLE command is used to delete complete data from
an existing table.
You can also use DROP TABLE command to delete complete table but it
would remove complete table structure form the database and you would need
to re-create this table once again if you wish you store some data.
Syntax
NOTE − You should be very careful while using this command because once a
table is deleted then all the information available in that table will also be lost
forever.
Syntax
DML commands are used for manipulating the data stored in the table and not
the table itself.
DML commands are not auto-committed. It means changes are not permanent
to database, they can be rolled back.
Command Description
insert to insert a new row
update to update existing row
delete to delete a row
merge merging two rows or two tables
INSERT Query
The SQL INSERT INTO Statement is used to add new rows of data to a table
in the database.
Syntax
There are two basic syntaxes of the INSERT INTO statement which are shown
below.
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
OR
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
Example
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
OR
You can create a record in the CUSTOMERS table by using the second syntax
as shown below.
INSERT INTO CUSTOMERS VALUES (7, 'Muffy', 24, 'Indore', 10000.00 );
UPDATE Query
The SQL UPDATE Query is used to modify the existing records in a table. You
can use the WHERE clause with the UPDATE query to update the selected
rows, otherwise all the rows would be affected.
Syntax
The basic syntax of the UPDATE query with a WHERE clause is as follows −
UPDATE table_name
SET column1 = value1, column2 = value2...., columnN = valueN
WHERE [condition];
You can combine N number of conditions using the AND or the OR operators.
The following query will update the ADDRESS for a customer whose ID
number is 6 in the table.
SQL> UPDATE CUSTOMERS SET ADDRESS = 'Pune' WHERE ID = 6;
DELETE Query
The SQL DELETE Query is used to delete the existing records from a table.
You can use the WHERE clause with a DELETE query to delete the selected
rows, otherwise all the records would be deleted.
Syntax
The basic syntax of the DELETE query with the WHERE clause is as follows −
DELETE FROM table_name WHERE [condition];
Example:
DELETE FROM CUSTOMERS WHERE ID = 6;
If you want to DELETE all the records from the CUSTOMERS table, you do
not need to use the WHERE clause and the DELETE query would be as follows
−
SQL> DELETE FROM CUSTOMERS;
DQL: Data Query Language
Data query language is used to fetch data from tables based on conditions that
we can easily apply.
Command Description
select retrieve records from one or more table
SELECT Query
The SQL SELECT statement is used to fetch the data from a database table
which returns this data in the form of a result table. These result tables are
called result-sets.
Syntax
Here, column1, column2... are the fields of a table whose values you want to
fetch. If you want to fetch all the fields available in the field, then you can use
the following syntax.
SELECT * FROM table_name;
Example:
1.where clause:
The WHERE clause specifies criteria that restrict the contents of the results table. You can
test for simple relationships or, using subselects, for relationships between a column and a set
of columns.
Using a simple WHERE clause, the contents of the results table can be restricted.
SELECT expressions FROM TABLES WHERE conditions;
Example:
2. GROUP BY
SQL GROUP BY statement is used to arrange identical data into groups. The GROUP BY
statement is used with the SQL SELECT statement.
The GROUP BY statement follows the WHERE clause in a SELECT statement and precedes the
ORDER BY clause.
The GROUP BY statement is used with aggregation function.
Syntax
SELECT column FROM table_name WHERE conditions GROUP BY column ORDER BY column ;
PRODUCT_MAST
Example:
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY;
Output:
Com1 5
Com2 3
Com3 2
3. HAVING
Syntax:
SELECT column1, column2 FROM table_name WHERE conditions GROUP BY column1,
column2 HAVING conditions ORDER BY column1, column2;
Example:
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMP
ANY HAVING COUNT(*)>2;
Output:
Com1 5
Com2 3
4. ORDER BY
Syntax:
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column1, column2... AS
C|DESC;
Where
Table:
CUSTOMER
12 Kathrin US
23 David Bangkok
34 Alina Dubai
45 John UK
56 Harry US
SELECT * FROM CUSTOMER ORDER BY NAME;
Output:
34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
SELECT * FROM CUSTOMER ORDER BY NAME DESC;
Output:
12 Kathrin US
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
1. COUNT FUNCTION
COUNT function is used to Count the number of rows in a database table. It can work on
both numeric and non-numeric data types.
COUNT function uses the COUNT(*) that returns the count of all the rows in a specified
table. COUNT(*) considers duplicate and Null.
Syntax
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Sample table:
PRODUCT_MAST
Example: COUNT()
SELECT COUNT(*) FROM PRODUCT_MAST;
Output:
10
SELECT COUNT(*) FROM PRODUCT_MAST WHERE RATE>=20;
Output:
SELECT COUNT(DISTINCT COMPANY) FROM PRODUCT_MAST;
Output:
Output:
Com1 5
Com2 3
Com3 2
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPAY
HAVING COUNT(*)>2;
Output:
Com1 5
Com2 3
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields
only.
Syntax
SUM()
or
SUM( [ALL|DISTINCT] expression )
Example: SUM()
SELECT SUM(COST) FROM PRODUCT_MAST;
Output:
670
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3;
Output:
320
Example: SUM() with GROUP BY
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3 GROUP BY COMPANY;
Output:
Com1 150
Com2 170
SELECT COMPANY, SUM(COST) FROM PRODUCT_MAST GROUP BY COMPANY HAVING SUM(CO
ST)>=170;
Output:
Com1 335
Com3 170
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function
returns the average of all non-Null values.
Syntax
AVG()
or
AVG( [ALL|DISTINCT] expression )
Example:
SELECT AVG(COST) FROM PRODUCT_MAST;
Output:
67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function
determines the largest value of all selected values of a column.
Syntax
MAX() or
MAX( [ALL|DISTINCT] expression )
Example:
SELECT MAX(RATE) FROM PRODUCT_MAST;
Output: 30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function
determines the smallest value of all selected values of a column.
Syntax
MIN()
or
MIN( [ALL|DISTINCT] expression )
Example:
SELECT MIN(RATE) FROM PRODUCT_MAST;
Output:
10