SQL Commands:: Syntax of SQL SELECT Statement
SQL Commands:: Syntax of SQL SELECT Statement
SQL commands are instructions, coded into SQL statements, which are used to communicate with
the database to perform specific tasks, work, functions and queries with data.
SQL commands can be used not only for searching the database but also to perform various other
functions like, for example, you can create tables, add data to tables, or modify data, drop the
table, set permissions for users. SQL commands are grouped into four major categories depending
on their functionality:
Data Definition Language (DDL) - These SQL commands are used for creating,
modifying, and dropping the structure of database objects. The commands are CREATE,
ALTER, DROP, RENAME, and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for storing,
retrieving,
modifying,
and
deleting
data.
These Data Manipulation Language commands are:SELECT, INSERT, UPDATE, and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for managing
changes affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for providing security to
database objects. These commands are GRANT and REVOKE.
Clause]
BY
[HAVING
[ORDER BY clause];
table-name is the name of the table from which the information is retrieved.
SQL Alias
clause]
clause]
SQL Aliases are defined for columns and tables. Basically aliases is created to make the column
selected more readable.
For Example: To select the first name of all the students, the query would be like:
first_name
AS
Name
FROM
student_details;
or
SELECT first_name Name FROM student_details;
In the above query, the column first_name is given a alias as 'name'. So when the result is
displayed the column name appears as 'Name' instead of 'first_name'.
Output:
Name
------------Rahul
Sharma
Anjali
Bhagwat
Stephen
Fleming
Shekar
Gowda
Priya Chandra
Syntax
{column
for
or
WHERE
expression}
clause
comparison-operator
with
Select
statement
value
is:
SQL Operators
There are two type of Operators, namely Comparison Operators and Logical Operators. These
operators are used mainly in the WHERE clause, HAVING clause to filter the data to be selected.
Comparison Operators:
Comparison operators are used to compare the column data with specific values in a condition.
Comparison Operators are also used along with the SELECT statement to filter data based on
specific conditions.
Comparision
Operators
Description
The LIKE operator is used to list all rows in a table whose column values match a specified pattern.
It is useful when you want to search rows to match a specific pattern, or when you do not know the
entire value. For this purpose we use a wildcard character '%'.
SQL ORDER BY
The ORDER BY clause is used in a SELECT statement to sort results either in ascending or
descending order. Oracle sorts query results in ascending order by default.
column-list
FROM
table_name
[WHERE
condition]
id
100
101
102
103
104
name
dept
age
Ramesh Electrical 24
Hrithik Electronics 28
Harsha Aeronautics 28
Soumya Electronics 22
Priya InfoTech 25
salary
location
25000 Bangalore
35000 Bangalore
35000 Mysore
20000 Bangalore
30000 Mangalore
For Example: If you want to sort the employee table by salary of the employee, the sql query
would be.
name
---------Soumya
Ramesh
Priya
Hrithik
Harsha
salary
---------20000
25000
30000
35000
35000
The query first sorts the result according to name and then displays it.
You can also use more than one column in the ORDER BY clause.