CLASS_XI_XII_SQL_SL_BASIC

  CLASS XI-XII

(SQL-BASIC SELECT STATEMENTS)


SQL TUTORIAL

If records are inserted in a table how to check the records are stored in a table.

SQL - SELECT Query

The SQL SELECT statement is used to fetch the data from a database table which returns this data in the form of a result table. These result tables are called result-sets.

Syntax

The basic syntax of the SELECT statement is as follows −
SELECT column1, column2, column  FROM  table_name;

Using ‘*’ (star) Sign: selected all the fields from the specified table name
Example:
SELECT * FROM CUSTOMERS;
CID
CustomerName
ContactName
Address
City
PostalCode
Country
1
Alfreds
Maria Anders
Obere Str. 57
Berlin
12209
Germany
2
Ana
Ana Trujillo
Avda. de la Constitución 2222
México D.F.
05021
Mexico
3
Antonio Moreno
Antonio Moreno
Mataderos 2312
México D.F.
05023
Mexico
4
Around
Thomas Hardy
120 Hanover Sq.
London
WA1 1DP
UK
5
Berglunds
Christina Berglund
Berguvsvägen 8
Luleå
S-958 22
Sweden

How to display the selective field or column using select statement.


Ans: By using the column name we can display the selective columns information.
Example:
Write a query to display roll_no , name and age from the table student.
SELECT ROLL_NO, NAME, AGE FROM STUDENT;

ROLL_NO
NAME
AGE
5
SAPTARHI
19
7
ROHIT
18
3
RIYANKA
20
2
PRATIK
19
8
NIRAJ
19
1
HARSH
18
6
DHANRAJ
20
4
DEEP
18

ORDER BY Clause

It is used to sort the data in ascending or descending order, based on one or more columns. The ORDER BY keyword sorts the records in ascending order by default.
Syntax:
SELECT column1, column2, ...FROM table_name ORDER BY column1, column2, ... ASC|DESC;
Table: student
Q. Write a query to sort the student table on the basis of name ascending order.
SELECT * FROM Student ORDER BY NAME;
ROLL_NO
NAME
ADDRESS
PHONE
AGE
4
DEEP
RAMNAGAR
XXXXXXXXXX
18
6
DHANRAJ
BARABAJAR
XXXXXXXXXX
20
1
HARSH
DELHI
XXXXXXXXXX
18
8
NIRAJ
ALIPUR
XXXXXXXXXX
19
2
PRATIK
BIHAR
XXXXXXXXXX
19
3
RIYANKA
SILIGURI
XXXXXXXXXX
20
7
ROHIT
BALURGHAT
XXXXXXXXXX
18
5
SAPTARHI
KOLKATA
XXXXXXXXXX
19
Q. Write a query to sort the student table in descending order of name.
……………………………………………………………
Output
ROLL_NO
NAME
ADDRESS
PHONE
AGE
5
SAPTARHI
KOLKATA
XXXXXXXXXX
19
7
ROHIT
BALURGHAT
XXXXXXXXXX
18
3
RIYANKA
SILIGURI
XXXXXXXXXX
20
2
PRATIK
BIHAR
XXXXXXXXXX
19
8
NIRAJ
ALIPUR
XXXXXXXXXX
19
1
HARSH
DELHI
XXXXXXXXXX
18
6
DHANRAJ
BARABAJAR
XXXXXXXXXX
20
4
DEEP
RAMNAGAR
XXXXXXXXXX
18

Q3.   Write a query to sort the student table record in descending order of roll_no and ascending order of name.

Followers

Popular Posts

Recent Posts