Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

SQL_3

The document provides SQL query examples demonstrating the use of 'BETWEEN' instead of 'AND' for range conditions, 'IN' instead of 'OR' for multiple conditions, and 'LIKE' for pattern matching in character columns. It also covers displaying calculations, renaming columns for display, and using the 'ORDER BY' clause for sorting results in ascending or descending order. Various examples illustrate these concepts using a 'students' table with attributes like name, age, fee, and class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

SQL_3

The document provides SQL query examples demonstrating the use of 'BETWEEN' instead of 'AND' for range conditions, 'IN' instead of 'OR' for multiple conditions, and 'LIKE' for pattern matching in character columns. It also covers displaying calculations, renaming columns for display, and using the 'ORDER BY' clause for sorting results in ascending or descending order. Various examples illustrate these concepts using a 'students' table with attributes like name, age, fee, and class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL

table students

rollno name age class fee sex dob percentage city

Use of between in place of and ( when we use same column for creating a range using ‘and’ condition then we can
use ‘between’ in place of ‘and’. When we use ‘between’ both values are included in the range)
In following both query we are finding age from 16 to 20

SQL> select name, age from students


Where age>= 16 and age<= 20 ;

SQL> select name, age from students


Where age between 16 and 20 ;

SQL> select name, age, fee from students


Where fee between 1800 and 3000 ;

Use of in in place of or ( when we use same column for creating a condition using ‘or’ then we can use ‘in’ in place of
‘or’.
In following both query we are finding age from 16 to 20

SQL> select name, age from students


Where age= 16 or age= 20 ;

SQL> select name, age from students


Where age in(16, 20) ;

SQL> select name, class, fee from students


Where name=’Sachin’ or name=’Sanjay’ ;

SQL> select name, class, fee from students


Where name in(’Sachin’,’Sanjay’) ;
Use of ‘like’ for character type columns. We use ‘like’ to fine some specific value in character column. We use ‘%’ with
like, ‘%’ indicates any character any number of times. And ‘_’ (underscore) indicates one character.

SQL> select name, fee from students


Where name='Sachin' ;

[name starting with ‘s’]


SQL> select name, fee from students
Where name like 'S%' ;

[name ending with ‘a’]


SQL> select name, fee from students
Where name like '%a' ;

[name ending with ‘i’]


SQL> select name, fee from students
Where name like ’%i' ;

[third char in name is ‘a’, two underscore without space and ‘a’]
SQL> select name, fee from students
Where name like ’_ _a%' ;

[Second char in name is ‘a’, one underscore without space ‘a’]


SQL> select name, fee from students
Where name like ’_a%' ;

[Name has only four character, four underscore without space ]


SQL> select name, fee from students
Where name like ’_ _ _ _' ;

[Name has only three character, three underscore without space ]


SQL> select name, fee from students
Where name like ’_ _ _' ;

[Name has ‘a’ in any place ]


SQL> select name, fee from students
Where name like ’%a%' ;
[we can display any calculation from integer column , 10% of fee column]
SQL> select name, fee, fee*0.1 from students;

[we can display any calculation from integer column, 10% of fee column of class ‘XII-A’ ]
SQL> select name, fee, fee*0.1 from students
Where class=’XII-A’ ;

[we can change any column name only for display purpose, using ‘ new column name’ method ]
SQL> select name ‘students name’ , fee, fee*0.1 ‘ discount’ from students;

Order By clause [used to display data in any order ascending or descending order] (by default ascending order for
descending order we have to write ‘desc’ with column name)

SQL> select name, age from students


Order by age ;

SQL> select name, age from students


Order by age desc ;

SQL> select name, age, fee from students


Order by fee ;

SQL> select name, age, fee from students


Order by fee desc ;

SQL> select name, age, class from students


Order by class ;

SQL> select name, age, class from students


Order by class desc;

[when values are same in any column then we can use any other column for further sorting ]

[ class and age is in ascending]


SQL> select name, age, class from students
Order by class, age ;

[ class is in ascending order and age is in descending order]


SQL> select name, age, class from students
Order by class, age desc;

[ class is in descending order and age is in ascending order]


SQL> select name, age, class from students
Order by class desc , age ;

[ class and age is in descending]


SQL> select name, age, class from students
Order by class desc , age desc;
[ class and age is in ascending]
SQL> select name, age, class from students
Where fee > 2700
Order by class, age ;

[ class is in ascending order and age is in descending order]


SQL> select name, age, class from students
Where fee > 2700
Order by class, age desc;

[ class is in descending order and age is in ascending order]


SQL> select name, age, class from students
Where fee > 2700
Order by class desc , age ;

[ class and age is in descending]


SQL> select name, age, class from students
Where fee > 2700
Order by class desc , age desc;

You might also like