![]() |
Selecting Rows and Columns SQL Projection![]() ![]() ![]() ![]() ![]()
Selecting Columns: SQL Projection The projection is a fundamental relational database operation that produces the table from selected columnsnot all columns of one or more existing tables. The order in which you want has to be specified. For example, if you enter: SQL > SELECT EMPNO ENAME FROM EMP; employee number and employee name alone are displayed. When you use asterisk to select all the columns of a table, the columns are displayed in the order in which they were defined when the table was created. If you wish to display the columns in a different order that as defined, then enter each column name instead of using an asterisk. By including the DISTINCT clause in the SELECT command we can eliminate duplicate rows in the result. For example, enter SQL SELECT DISTINCT JOB 2 FROM EMP; This will list all the distinct jobs in EMP. Selection, another important relational database operation, selects and displays
rows of one or more tables that satisfy some criteria. For example, if you want
to list the employee number, name and job of all the employees *in department
2, enter: 2 FROM EMP 3 WHERE DEPTNO = 2; To eliminate duplicate rows in the result, we include the DISTINCT clause in the SELECT command. To list all the distinct jobs in ElP, enter. SQL > SELECT DISTINCT JOB 2 FROM EMP: <b> Combining Selection and Projection</b> To select specific rows from a table, then we include a WHERE clause in the SELECT statement. The general format of this statement is as: SELECT < colunm name(s) > FROM < table name > WHERE < condition > To view the employee number, name and job of all the employee in department 3 enter. SQL > SELECT EMPNO, ENAME, JOB 2 FROM EMP 3 WHERE DEPTNO = 3; Like logical expression above, we can also have compounded logical expression using AND and OR logical operators. For example, if you want to find the employee numbers, names and joining dates who joined the organization on of after 1 st JUNE 1996, then enter: SQL > SELECT EMPNO, ENAME, JOIN DT 2 FROM EMP 3 WHERE JOB = "MANAGER" 4 AND JOIN DT > ='O 1 jun 96'; All operators in SQL*Plus are arranged in a hierarchy, as in high school Algebra.
This hierarchy determines SQL > SELECT ENAME, JOB, DEPTNO FROM EMP 2 WHERE JOB ='MANAGER' 3 AND DEPTNO! = 1; If you wish to select rows in a table in which a column contains a value within a specified lower and upper limit, we use the BETWEEN operator. For example, if you wanted to know the names, jobs and joining dates of employees who jointed the organization between 1st January '89 and 1st January'94, then the query will be as follows: SQL > SELECT ENAME, JOB, JOIN DT FROM EMP 2 WHERE JOIN_DT BETW EEN'01 JAN 89'AND'Ol JAN 94'; You can also negate BETWEEN like other operators, with NOT. The clause NOT BETWEEN means that only rows outside the range will be selected. The IN operator, similar to BETWEEN is a shorthand solution to certain SQL problems. With IN, you can select rows that match one of the values in a list. For example, to find the employees who are analysis, engineers and salesmen, we enter: SQL > SELECT ENAME, JOB, DEPTNO F ROM EMP 2 WHERE JOB IN('ANALYST','ENGINEER, 'SALESMAN'); SQL *Plus provides you with a marvellous pattern matching operator called LIKE. LIKE is able to search through the rows of a database column for values that look like a pattern you describe. It uses special characters to denote which kind of matching you wish to do: a percent slgn,(%o) called wild card, and an underline(un(ierscore) character (_) called a position market. The wild card is used for representing any sequence of zero or more characters and the position marker is used for representing any sequence of zero or more characters. To look for the names, jobs and employee numbers of all employees whose names begin with K, the query will be as follows: SQL > SELECT EMPNO, ENAME, JOB FROM EMP 2 WHERE ENAME LIKE K% <b>The Order by Clause</b> With the ORDER BY clause you can order rows in ascending or descending order, by multiple columns or with null values. Consider the following example: SQL > SELECT EMPNO, ENAME, JOB FROM EMP 2 WHERE DEPTNO = I 3 ORDER BY EMPNO; As seen above, the ORDER BY clause must be placed last in the SELECT command
statement.
|
|
Domain NamesSQL Database Management Systems Relational Database Management System Properties of RDBMS Client Server Computing Oracle Relational Database Database Structure and Space Management SQL Data Types How to Use Data Types? What is Operators and Conditions Character OperatorsOperator Precedence Data Retrieval Using SQL Plus SQL Data Definitions Data Retrieval Using Select SQL Operator Precedence Selecting Rows and Columns The Group by Clause
Having Clause Union, Union all, Intersact and Minus Commands Playing with Numbers Date Functions Example of Date Arithmetic Working with Null Values
Joining Tables and Subqueries Views Synonyms Indexes Clusters Sequences Formatting Query Results with SQL Plus Data Integrity The Optimizer How Oracle Optimizes SQL Statements Evaluating Expression and ConditionsOptimization Hints PL-SQL PL-SQL Architecture Error Reporting Functions Character Functions Composite Data Types PL-SQL Structures How to use PL-SQL Structures Normalization Operator Precedence Cursor Error Handling Database Triggers Types of Triggers Locking Sub Programs Packages New and Improved Data Types Improved Select Statement Advanced Quering Improved Scalability Improved Performance Via Partitioning Object Relational Features Heterogeneous Data Access Improved Security Administration New Data Types Improved Select Statement Changes to the Select Statement Improved Scalability Data Partitioning Oriented Toward Objects Character Functions Creating Object Types Created Nested Tables Oracle8 and Distributed Database Oracle8 Database Management and Security Distributed Database Invoking Export Invoking ImportWeb DesignWeb HostingE Commerce |
| Home | Web Hosting | Web Design | Sitemap |
| Copyright (C) 2007. Web Domain design hosting. All rights reserved. |