WELCOME
PRACTICAL
SESSION
N DS
MA
C OM
SQL QUERY PROCESSING
SELECT
Syntax:
SELECT <column-name1> , <column-name2>…
FROM <table-name>;
For example:-
1. SELECT NAME, GENDER FROM STUDENT;
2. SELECT * FROM STUDENT;
3. SELECT NAME, ROLLNO, DOB, MARKS FROM
STUDENT;
CLAUSES
IN
MYSQL
1. WHERE clause
This clause is used to specify the condition with
the SELECT statement based on which rows of a
table are displayed.
For example,
• mysql> SELECT * FROM STUDENT WHERE
ROLLNO<=8;
• mysql> SELECT * FROM STUDENT WHERE
GENDER=‘M’ AND MARKS<95;
2. DISTINCT clause
This clause is used to retrieve and
display distinct values from a column
of a table.
Syntax: SELECT DISTINCT <column-
name> from <table-name>;
For example,
SELECT DISTINCT STREAM FROM STUDENT;
3. BETWEEN clause
This clause is used to define the range of values
within which the column values must fall to make
a condition true. Range includes between upper
and lower values.
3. BETWEEN clause
Syntax for BETWEEN:
mysql>SELECT <column_name(s)> FROM
<table_name>WHERE <column_name>
BETWEEN <value1> AND <value2>;
For example,
• mysql> SELECT * FROM STUDENT WHERE MARKS
BETWEEN 90 AND 95;
• mysql> SELECT Rollno, Name, Marks FROM student
WHERE Marks BETWEEN 80 AND 100;
3. BETWEEN clause
NOT BETWEEN
The NOT BETWEEN operator works opposite to the BETWEEN
operator. It retrieves the rows which do not satisfy the BETWEEN
condition.
For example:-
mysql> SELECT ROLLNO, NAME, MARKS FROM STUDENT
WHERE MARKS NOT BETWEEN 80 AND 100;
4. IN clause
This clause is used to select values that match any
value in a list of specified values.
Syntax for IN:
SELECT <column_name(s)> FROM <table_name>
WHERE <column_name> IN (value1,value2,...);
For example,
• mysql> SELECT * FROM STUDENT WHERE STREAM
IN(‘SCIENCE’,’VOCATIONAL’);
• mysql> SELECT * FROM STUDENT WHERE STREAM
IN (‘SCIENCE’, ‘COMMERCE’, ‘HUMANITIES’);
4. IN clause
NOT IN
The NOT IN operator works opposite to IN operator. It
matches, finds and returns the rows that do not match
the list.
For example,
mysql> SELECT * FROM STUDENT WHERE STREAM NOT IN
(‘SCIENCE’, ‘COMMERCE’, ‘HUMANITIES’);
5. IS NULL/ NOT NULL
operator
This clause/ operator is used to select rows in which
the specified column is NULL (or is NOT NULL)
For example,
1. SELECT * FROM STUDENT WHERE STREAM IS NOT
NULL;
2. SELECT * FROM STUDENT WHERE MARKS IS NULL;
6. LIKE operator
This clause/operator is used for pattern matching of
string data using wildcard characters % and _ .
Syntax for LIKE:
SELECT <column_name(s)> FROM <table_name>
WHERE <column_name> LIKE <pattern>;
%E%--- matches any string containing ‘E’.
%A– matches any string ending with ‘A’.
‘_ _ _g’ matches any string that is 4
characters along with 3 characters in the
beginning but ‘g’ as the 4th character.
SQL OPERATORS
SQL OPERATORS
While working with SELECT statement using WHERE
clause, condition-based query can be carried out using
four types of SQL operators:
(a) Arithmetic Operators
(b) Relational Operators
(c) Logical Operators
(d) Special Operators
ARITHMETIC OPERATORS
Arithmetic operators are used to perform simple arithmetic
operations like addition (+),subtraction (–), multiplication (*),
division (/) and modulus (%). These operators are used with
conditional expressions and for performing simple
mathematical calculations.
• mysql> SELECT 5 + 10 FROM DUAL;
• mysql> SELECT 5 * 4 FROM DUAL;
• mysql> SELECT 47 % 5 FROM DUAL
RELATIONAL OPERATORS
A relational (comparison) operator is a mathematical
symbol which is used to compare two values. It is used
to compare two values of the same or compatible data
types.
Different types of comparison operators in SQL:
= Equal to
> Greater than
< Less than
>= than or equal to
<= Less than or equal to
<>,!= Not equal to
For example:-
• mysql> SELECT ROLLNO, NAME, MARKS FROM STUDENT WHERE
MARKS>=90;
• mysql> SELECT * FROM STUDENT WHERE STREAM <> ‘COMMERCE’;
LOGICAL OPERATORS
The SQL logical operators are the operators used to
combine multiple conditions to narrow the data
selected and displayed on the basis of the condition
specified in an SQL statement. Logical operators are
also known as Boolean operators. The three logical
operators in SQL are—AND, OR and NOT operator.
LOGICAL OPERATORS
For example:-
• mysql> SELECT * FROM STUDENT WHERE MARKS > 80
AND GENDER= ‘M’;
• mysql> SELECT ROLLNO, NAME, STREAM FROM STUDENT
WHERE STREAM= ‘SCIENCE’ OR STREAM= ‘COMMERCE’;
• mysql> SELECT NAME, MARKS FROM STUDENT WHERE
NOT (STREAM = ‘VOCATIONAL’);
Comments in SQL
• Comments beginning with – (followed by a
space): The two dash lines indicate a single line
comment in SQL statements. These single-line
comments are basically used to show the
comments at the start and end of a program.
Comments in SQL
• Comments beginning with #: The comments
begin with ‘#’ symbol followed by the text to be
displayed for the user’s information. . This text
cannot extend to a new line and ends with a line
break.
Comments in SQL
• Comments beginning with /*: Multi-line
comments begin with a slash and an asterisk (/*)
followed by the text of the comment. This text
can span multiple lines. The comment ends with
an asterisk and a slash (*/).
Comments in SQL
For example,
mysql> SELECT ROLLNO, NAME, STREAM
/* This statement shall display the records of all those students who are
in Science stream and have secured marks more than 75. */
FROM STUDENT # student table in use
WHERE STREAM= ‘SCIENCE’ AND MARKS > 75; --condition for projection
SQL ALIASES
• COLUMN ALIASES are used to make column headings in the query
result set easier to read.
Syntax for column alias:
SELECT <column-name> AS <“alias_name”> FROM <table_name>
WHERE [<condition>];
For example:-SELECT NAME AS “STUDENT_NAME”, DOB AS
“DATE_OF_BIRTH” FROM STUDENT;
SQL ALIASES
• TABLE ALIASES are used to shorten a table name by giving
an easy alternate name, making it easier to read or when
performing a self-join (i.e., listing the same table more than
once in the FROM clause).
Syntax for table alias:
SELECT <columnname1>, <columnname2>....FROM
<table_name> AS <alias_name>WHERE [<condition>];
SORTING IN SQL—ORDER BY
The SQL ORDER BY clause is used to sort the data in ascending or
descending order based on one or more columns.
Syntax for ORDER BY clause:
SELECT <column-list> FROM <table_name> [WHERE <condition>]
ORDER BY <column_name> [ASC|DESC];
Here, WHERE clause is optional.
SORTING IN SQL—ORDER BY
For example:-
• mysql> SELECT ROLLNO, NAME, MARKS FROM STUDENT
ORDER BY NAME;
• mysql> SELECT ROLLNO, NAME, MARKS FROM STUDENT
ORDER BY MARKS DESC, NAME;
• mysql> SELECT ROLLNO, NAME, MARKS AS
MARKS_OBTAINED FROM STUDENT ORDER BY
MARKS_OBTAINED;