Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Data Manipulation Language

A DML statement is executed when you:


 Add new rows to a table
 Modify existing rows in a table
 Remove existing rows from a table

INSERT INTO Command- Adding records


This command is used to add rows in the table.
Syntax:
INSERT INTO <table name> [(Column_name1, Column_name2, ......Column_name n)]
VALUES(value1,value2,value3,….,value n);
OR
INSERT INTO <table name> VALUES (value1,value2,value3,….,value n);
Examples
INSERT INTO student VALUES (111,"Anu Jain", 12,"A", 2500);
INSERT INTO student (ADNO, Name, CLASS) VALUES (777,' LEENA', 'B');

Inserting Rows with Null Values


1. Implicit method: Omit the column from the column list
INSERT INTO student (ADNO, Name) VALUES (777,' LEENA');

2. Explicit method: Specify the NULL keyword.


INSERT INTO student VALUES (111,"Anu Jain", 12,"A", NULL);

Data Updation- UPDATE Command

This command is used to implement modification of the data values.


Syntax:
UPDATE <table name>
SET <column name1>=new value, <column name>=new value etc
[WHERE <condition>];

Example:
1. Increase fees value by 500.
UPDATE student
SET fees = fees + 500;
2. Increase the fees value by 100 for adno 222.
UPDATE student
SET fees = fees+100
WHERE adno = 222;

Data deletion - DELETE Command


This command is used to remove information from a particular row or rows.
Syntax:
DELETE
FROM <table name>
[WHERE <condition>];
Example:
1. Remove information of student whose adno is 444 .
DELETE
FROM student
WHERE adno = 444;
2. Remove all records.
DELETE
FROM student;

SELECT Command
This command is used to view table information from SQL database.
Syntax:
SELECT (*/field list)
FROM <table name>
[WHERE <condition>];
*- to get all columns
Example:
1. Display student table information.
SELECT *
FROM student;
This will display all information of the particular table (student) in the database.
2. To display name and class of students in table student.
SELECT name, class
FROM student;
3. To display names of 10th class students.
SELECT name
FROM student
WHERE class = 10;

Operators used in SQL commands:


Arithmetic operator takes two operands and performs a mathematical calculation and can be used
only in SELECT command.
The arithmetic operators used in SQL are:
+ Addition
- Subtraction
* Multiplication
/ Division
Example
1) SELECT FirstName + SecondName
FROM Name;
Output
FirstName + SecondName
AnuJain
MadhuBhattia

2) SELECT Basic + DA
FROM Salary;
Output:
Basic + DA
30000
42000
3) SELECT Basic + DA as "NET PAY"
FROM Salary;
Output:
NET PAY
30000
42000

4) Select DA-100
From salary;
Output:

DA-100
4900
6900

5) Select DA*100
From salary;
Output:

DA*100
500000
700000

6) Select DA/100
From salary;
Output:
DA/100
50
70

Relational operators:
Relational operators are -
< less than
> greater than
< = less than or equal to
> = greater than or equal to
= equal to
! = not equal to
They are used in ‘where’ clause
1) Display students' name, who are paying below 3000 fees.
SELECT name FROM student WHERE fees<3000;

2) Display students' name, who are paying above or equal to 3000 fees.
SELECT name FROM student WHERE fees>=3000;

3) Display students' information, who are not in class 10


SELECT * FROM student WHERE class! = 10;
Logical operators:
Logical operators are used to merge more than one condition. They are used in ‘where’ clause
Logical operators are:
AND
OR
NOT
Example:
1. Display information of students in class 11B.
SELECT * FROM student WHERE class = 11 AND section = 'B';
2. Display 11th and 12th class students' information.
SELECT * FROM student WHERE class =11 OR class=12;
3. Display students' information, who are not in 10th class.
SELECT * FROM student WHERE NOT class = 10;

LIKE OPERATOR
LIKE OPERATOR is used to search a value similar to specific pattern in a column using wildcard operator.
There are two wildcard operators - percentage sign (%) and underscore ( _ ). The percentage sign represents
zero, one, or multiple characters, while the underscore represents a single number or character.
The symbols can be used in combinations.
For example:
1. Display the names that start with letter "A".
SELECT name FROM student WHERE name LIKE "A%";
2. Display names, whose name's second letter is 'o'.
SELECT name FROM student WHERE name LIKE "_ o%";
3. Display names, whose name has 7 characters.
SELECT name FROM student WHERE name LIKE "_______";
Here, _ replaces only one character.

IN Operator
It allows us to specify multiple values in a WHERE clause.
For example:
Display students' information, who are in section A and B.
SELECT * FROM student WHERE section IN ("A","B");

BETWEEN Operator

The BETWEEN operator is used to test whether or not a value is "between" the two values stated after the
keyword BETWEEN.
For example:
Display students' information, who are paying fees between 2500 and 3500.
SELECT * FROM student WHERE fees BETWEEN 2500 AND 3500;
[Note: In the above Query 2500 and 3500 is also included]
DISTINCT
The default display of queries is all rows, including duplicate rows.
The DISTINCT keyword is used to remove duplicate values in a particular column.
For example:
Display class in student table.
SELECT class FROM student;
Class
12
11
12
10
12
11

Display different classes from student table.


SELECT DISTINCT class FROM student;

Class
12
11
10
ORDER BY
This command is used to arrange values in ascending or descending order. The ORDER BY clause
comes last in the SELECT statement.

For example:
SELECT *FRO M student
ORDER BY fees ASC; # 'asc' for ascending order.
Default display is in ascending order

SELECT *
FROM student
ORDER BY fees DESC; # 'desc' for descending order.

Sorting by Multiple Columns


SELECT * FROM student
ORDER BY class DESC, Sec, name ASC;
Aggregate functions
Aggregate functions are used to implement calculation based upon a particular column. These
functions always return a single value.
Aggregate functions are:
1. SUM()
2. AVG()
3. MAX()
4. MIN()
5. COUNT()

SUM()
This function is used to find the total value of a particular column.
Example:
SELECT SUM (fees) FROM student;

SUM (fees)
17500
AVG()
This function is used to find the average value of a particular column.
Example:
SELECT AVG (fees) FROM student;

AVG (fees)
2916.6666
MAX()
This function is used to find the maximum value of a particular column.
Example:
SELECT MAX (fees) FROM student;
MAX (fees)
4500
MIN()
This function is used to find the minimum value of a particular column.
Example:
SELECT MIN (fees) FROM student;

MIN(fees)
2000
COUNT()
This function is used to find the number of values (ie. number of rows) of a particular column.
Example:
SELECT COUNT (fees) FROM student; # COUNT(expr) returns the number of non-null rows.
(or)
SELECT COUNT (*) FROM student; # COUNT(*) returns the number of rows in a table.

COUNT (fees)
6
(or)
COUNT (*)
6

Handling NULL Values


 SQL supports a special value called NULL to represent a missing or unknown
value.
 Any arithmetic operation performed with NULL value gives NULL.
 For example: 5 + NULL = NULL because NULL is unknown hence the result is
also unknown. In order to check for NULL value in a column, we use IS NULL
operator.
GROUP BY

GROUP BY function is used to group rows of a table that contain the same values in a
specified column. We can use the aggregate functions (COUNT, MAX, MIN, AVG and
SUM) to work on the grouped values. HAVING Clause in SQL is used to specify
conditions on the rows with Group By clause.
Exam ple:
1. Display number of students in each class.
SELECT class, count (*)
FROM student
GROUP BY class;

Note:
Group functions ignore null values in the column.

HAVING clause
‘where' clause is used only to place condition on the selected columns for
individual rows, whereas the 'HAVING' clause is used to place condition on
individual groups created by 'group by' clause.
If WHERE and HAVING both are used in SELECT statement, the WHERE will
be executed first
Example:
Display total fees collected from each class
SELECT class, SUM(fees) FROM student GROUP BY class;
class SUM(fees)
11 14800
12 14500
Display sum of fees which is more than 5000 for each class
SELECT class, sum (fees)
FROM student
GROUP BY class
HAVING sum (fees)>5000;
Equi Join
Equi Joins are used to give information in different tables. It is a special type of join in
which we use only equality operator.
For example
SELECT *
FROM product, customer
WHERE product.product_no = customer. product_no;
(or)
SELECT *
FROM product p, customer c
WHERE p.product_no=c.product_no;
SQL Non-equi Join
The non-equi join is a type of join in which, we use only relational operators
except equal operator. These include >, <, >=, <= and !=.
For example
SELECT *
FROM product,customer
WHERE product.product_no!=customer.procuct_no;

You might also like