Select Query (/database/sql-tutorial/6-select-query)
Page 1 of 3
The SELECT query is the most often used query in any database.
So , learning this query is very important. Even if you don't write it (http://www.addthis.com/bookmark.php)
manually, as some tools make your task easier by providing code assists. But this query helps you to
understand many aspects of database concepts, ranging from DBA task to PL/SQL. I hope this tutorial
helps learning SELECT query faster and easier.
We will be referring tables from default schema/user SCOTT in Oracle.
The generalized select query looks like :
SELECT
[ DISTINCT| ALL]
{ * | [columnName [ AS newColumnName ] ] ,
[columnName1 [ AS newColumnName1]],
.........,
.........,
}
FROM tableName [ALIAS][,]
[WHERE <condition>]
[GROUP BY columnlist ] [HAVING <condition>]
[ORDER BY columnlist]
We will start with simple query and then keep on building complex queries.
1. Select all the details of the employee from emp table.
SELECT * FROM emp;
2. Select only the names of all the employees in emp table;
SELECT ename FROM emp;
3. Select records using multiple columns;
SELECT eno,ename,sal FROM emp;
4. Select records Using alias;
SELECT eno,ename,salary AS sal FROM emp;
a. Select records using multiple alias
SELECT eno AS empno,ename AS empname ,sal AS salary FROM emp;
b. Using functions to enhance alias
SELECT count(sal) as totalsal FROM emp;
5. Derived or Computed fields: Columns values were manipulated as it gets retrived.
a. Find the monthly salary of employee,( The salary stored is on Annum basis).
SELECT sal / 12 FROM emp;
b. Using Alias to decorate derived or computed fields.
SELECT sal / 12 AS monthly_salary FROM emp
c. Calculate the sum of monthly salary and the commissions of the employee.
SELECT ename,(sal / 12) + nvl(comm,0) AS monthsalwithcomm FROM emp;
Pages:
2 (/database/sql-tutorial/6-select-query?limit=1&start=1)
3 (/database/sql-tutorial/6-select-query?limit=1&start=2)
Prev (/database/sql-tutorial/7-complex-queries-in-sql)
Most Read Articles
Computer Awareness Home (/quiz/computerawareness)
Complex Queries in SQL ( Oracle )
(/database/sql-tutorial/7-complex-queries-in-sql)
Struts2 Interview Questions and Answers
(/java/struts2/interview-questions)
Quiz Home (/quiz)
Java Programming Interview Questions
(/java/core-java/33-interview-questions)