Вы находитесь на странице: 1из 4

NULL

Sometimes, you don't know the value for a column. In a table, you can store these unknowns as
NULL. NULL means a value that is unavailable, unassigned, unknown or inapplicable. NULL is
not the same as zero or a space or any other character.
2 ways to insert NULL value in the table
1. insert into table_name values (value1, value2, null, value4);
2. insert into table_name (column1,column2,column4) values (value1, value2, value4);

Since Null does not mean 0 therefore we cannot use operators like =, >, <, etc for searching for Null
values in a column. To search for NULL values in the table we use IS NULL
example – select * from student where fees is null;
SELECT * FROM Student WHERE Name IS NULL;

NULL does not represent a value of 0 therefore


Null + 10 = null (and 0 + 10 = 10)
Thus for comparisons and calculations, null value in a column cannot be used.

NOT NULL values in a table can be searched using IS NOT NULL.


e.g. SELECT * FROM Employee WHERE Commission IS NOT NULL;

RETRIEVING INFORMATION FROM THE TABLE

Using the keyword DISTINCT, the duplicate values of the rows from a specific column can be
eliminated.
e.g.
Select Distinct Subject from teachers;OR
Select Distinct(Subject) from teachers;

DELETE STATEMENT – To delete specific rows


Example Table: Student
Name Class Sec
Hema XI A
Raj XI A
Rahul X B
Prakash X C
Priya XII A

Q1. Delete student Rahul’s records

Ans DELETE FROM Student tablename

WHERE name = ”Rahul”;

Value in the column

Name of the column

The deletion will be done only in the database. This will not be displayed on the screen. To display the remaining table on
screen we use:
SELECT * FROM Student;

OUTPUT:-

Name Class Sec


Hema XI A
Raj XI A
Prakash X C
Priya XII A

Q2. Delete the records of XI A students

Ans DELETE FROM Student tablename

WHERE class = ”XI” AND sec = ”A”;

Value in the column

Name of the column

Remaining table of the database is as follows

Name Class Sec


Prakash X C
Priya XII A

Q3. Delete all rows from the table Student

Ans DELETE FROM Student;

Remaining table of the database is as follows

Name Class Sec

DROP STATEMENT – To delete WHOLE table (Rows & columns)

DROP TABLE ;
Student tablename

After this statement Student table is fully deleted and we can no longer access it.

ORDER BY - Sorting the Results


The result obtained using SELECT statement is displayed in the order in which the rows were
entered in the table using the INSERT INTO statement. The results of the SELECT statement can be
displayed in the ascending or descending values using ORDER BY clause.
Example:- Table: Class
RollNo Name Gender Marks
1 Samidsha F 70
2 Akaash M 63
3 George M 80
4 Geet F 70

Q1. Display data of students in ascending order of their marks.


Ans SELECT * FROM Class ORDER BY Marks;
OR can be written as SELECT * FROM Class ORDER BY Marks asc;
OUTPUT:-
RollNo Name Gender Marks
2 Akaash M 63
1 Samidsha F 70
4 Geet F 70
3 George M 80

Q2. Display data of students in ascending order of their names.


Ans SELECT * FROM Class ORDER BY Name;
OR can be written as SELECT * FROM Class ORDER BY Name asc;
OUTPUT:-
RollNo Name Gender Marks
2 Akaash M 63
4 Geet F 70
3 George M 80
1 Samidsha F 70

Q3. Display data of students in descending order of their names.


Ans SELECT * FROM Class ORDER BY Name desc;
OUTPUT:-
RollNo Name Gender Marks
1 Samidsha F 70
3 George M 80
4 Geet F 70
2 Akaash M 63

Q4. Display data of students in descending order of their marks.


Ans SELECT * FROM Class ORDER BY marks desc;
OUTPUT:-
RollNo Name Gender Marks
3 George M 80
1 Samidsha F 70
4 Geet F 70
2 Akaash M 63

Q5. Display data of students in descending order of their Roll numbers.


Ans Do urself
Suppose we want to display all the rows of the table Student in ascending order of Marks. But if
several students have the same value for Marks, for them we want the display to be in ascending
order of names.
To order results on more than one column we write:
SELECT * FROM Student ORDER BY Marks,Name;
OUTPUT:-
RollNo Name Gender Marks
2 Akaash M 63
4 Geet F 70
1 Samidsha F 70
3 George M 80

Q6. Display data of students in descending order of their marks but if several students have the
same value for marks, for them the display is in ascending order of names.
Ans SELECT * FROM Class ORDER BY marks desc, name;
OUTPUT:-
RollNo Name Gender Marks
3 George M 80
4 Geet F 70
1 Samidsha F 70
2 Akaash M 63

Вам также может понравиться