SQL Commands 8

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

SQL Part 2

Nahida Nazir
Select distict

 The SELECT DISTINCT statement is used to return only


distinct (different) values.

 Inside a table, a column often contains many duplicate


values; and sometimes you only want to list the
different (distinct) values.

Nahida Nazir
Nahida Nazir
 create table j (id int, name varchar(21));
 insert into j values (21, 'raj')
 insert into j values (21, 'piya')
 insert into j values (22, 'rohan')
 select distinct id from j

Nahida Nazir
NULL Value
 A field with a NULL value is a field with no value.
 If a field in a table is optional, it is possible to insert a
new record or update a record without adding a value to
this field. Then, the field will be saved with a NULL value.

Nahida Nazir
Syntax

 SELECT column_names
FROM table_name
WHERE column_name IS NULL;

Nahida Nazir
IS NOT NULL Syntax
 SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

Nahida Nazir
The IS NULL Operator

 This operator is used to test for the empty values


 create table j (id int, name varchar(21));
 insert into j values (21, 'raj')
 insert into j values (21, 'piya')
 insert into j values (NULL, 'rohan’)
 Result= Null for rohan

Nahida Nazir
IS NOT NULL operator

 The IS NOT NULL operator is used to test for non-empty


values (NOT NULL values)

Nahida Nazir
 SELECT id, name
 FROM j
 WHERE id IS NULL;

Nahida Nazir
Adding 100 to an existing id

 SELECT id, name, id + 100


 AS "id + 100" FROM ji;

Nahida Nazir
 SELECT id, name, id - 100
 AS "id - 100" FROM ji;

Nahida Nazir
Multiplication

 SELECT id, name, id * 100


 AS "sub" FROM ji;

Nahida Nazir
SQL AND and OR operators

 In SQL, the AND & OR operators are used for filtering the data and getting
precise results based on conditions. The SQL AND & OR operators are also used
to combine multiple conditions. These two operators can be combined to test for
multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement.

 When combining these conditions, it is important to use parentheses so that the


database knows what order to evaluate each condition.

 The AND and OR operators are used with the WHERE clause.
 These two operators are called conjunctive operators.
 AND Operator:

Nahida Nazir
AND Operator:
 This operator displays only those records where both the conditions
condition1 and condition2 evaluates to True.

 Syntax:

 SELECT * FROM table_name WHERE condition1 AND condition2


and ...conditionN;

Nahida Nazir
Example

 create table j8 (id int, name varchar(21), address


varchar(21));
 insert into j8 values (21, 'raj', 'jk')
 insert into j8 values (121, 'piya', 'punjab')
 select *from j8 where id= 121 and name='piya' and
address= 'punjab'

Nahida Nazir
OR Operator:
 This operator displays the records where either one of the conditions
condition1 and condition2 evaluates to True. That is, either condition1 is True
or condition2 is True
 To fetch all the records from the Student table where NAME is Ram or NAME
is SUJIT.
 SELECT * FROM Student WHERE NAME = ‘piya' OR NAME = ‘rahul';

Nahida Nazir
 Take an example to fetch all the records from the Student table
where Age is 18 NAME is Ram or RAMESH.
 SELECT * FROM Student WHERE Age = 18 AND (NAME = 'Ram' OR
NAME = 'RAMESH');

Nahida Nazir
Not operator

 The NOT operator displays a record if the condition(s) is NOT TRUE.


Synatx
SELECT * FROM j8
 WHERE NOT id =21;

Nahida Nazir
 To get the employees who work in the department id 5 and with a salary not
greater than 5000.
 SELECT
 id,
 name,
 salary
 FROM
 employees
 WHERE
 id = 5
 AND NOT salary > 5000
 ORDER BY
 salary;

Nahida Nazir

You might also like