SQL Notes
SQL Notes
SQL Notes
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;
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;
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;
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
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.
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
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;