SQL Commands
SQL Commands
SQL Commands
Constrains
In SQL, we have the following constraints:
1. NOT NULL - To check a column cannot store NULL value.
2. PRIMARY KEY - To check that a column have an unique identity which helps to
find a particular record in a table.
Syntax:
CREATE TABLE<table name>
(<column name1> <data type>[size][constraints],
<column name2> <data type>[size][constraints],
.
<column name n> <data type>[size][constraints]);
Column Name Data Type Size Constraints
NET PAY
SELECT Basic + DA as "NET PAY"
30000
FROM Salary;
Output: 42000
Relational operators
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;
Logical operators are also possible only in 'where clause' and are used to
merge more than one condition. Logical operators are:
1. AND
2. OR
3. NOT
Example:
Display information of students in class 11B.
Adno Name Class Section Fees
SELECT *
FROM student 222 Mohit 11 B 4500
Sharma
WHERE class = 11 AND section = 'B';
666 Rohan 11 B 2500
Sharma
Logical operators
Logical operators are also possible only in 'where clause' and are used to
merge more than one condition. Logical operators are:
For example:
Display the names that start with letter "A".
Name
SELECT name
FROM student
WHERE name LIKE "_ o%";
Here, % replaces one or more than one character and _ replaces only one
character.
Name
Mohit Sharma
Rohan Sharma
Display names, whose name has 7 characters.
SELECT name
FROM student
WHERE name LIKE " ";
Name
Nandini
IN Operator
The IN operator 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 class IN ("A","B");
Adno Name Class Section Fees
111 Anu Jain 12 A 2500
222 Mohit Sharma 11 B 4500
333 K.P.Gupta 12 B 3000
444 Ajit Kumar 10 A 2000
666 Rohan Sharma 11 B 2500
BETWEEN Operator
The BETWEEN operator is used to test whether a value (stated before the keyword
BETWEEN) 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]
Adno Name Class Section Fees
111 Anu Jain 12 A 2500
333 K.P.Gupta 12 B 3000
'asc' for ascending order. Without asc also the list is displayed with
ascending order only.
SELECT *
FROM student ORDER BY fees DESC;
'desc' for descending order. If the 'desc' is not given, the list will be
displayed with ascending order.
Aggregate functions
1.SUM()
2.AVG()
3.MAX()
4.MIN()
5.COUNT()
GROUP BY
The SQL GROUP BY is a clause that enables SQL aggregate functions for
grouping of information. (ie. GROUP BY clause is used in collaboration with
the SELECT statement to arrange identical data into groups.). This clause is
used whenever aggregate functions by group are required.
For example:
Display number of students in each class. SELECT count (*), class
FROM student GROUP BY class;
Display sum of fees for each class. SELECT class, sum (fees)
FROM student GROUP BY class;
Count (*) Class Class Sum (fees)
2 11 11 7000
3 12 12 8500
1 10 10 2000
Having clause
As mentioned earlier, the 'where' clause is used only to place condition on the
selected columns, whereas the 'HAVING' clause is used to place condition on
groups created by 'group by' clause, because here the 'WHERE' clause is not
useable.
Example:
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;
Class Sum (fees)
11 7000
12 8500
DISTINCT
The DISTINCT keyword is used to remove duplicate values in a column.
For example:
Display class in student table.
SELECT class
FROM student;
Class Class
12 12
11
11
12
10
10
12
11
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:
Increase fees value by 500. UPDATE student
SET fees = fees + 500; Adno Name Class Section Fees
111 Anu Jain 12 A 3000
222 Mohit Sharma 11 B 5000
333 K.P.Gupta 12 B 3500
444 Ajit Kumar 10 A 2500
555 Nandini 12 C 3500
666 Rohan Sharma 11 B 3000
•Increase the fees value by 100 for adno 222. UPDATE student
SET fees = fees+100 WHERE adno = 222;
SELECT *
FROM product,customer
WHERE product.product_no!=customer.procuct_no;