标签:remove 存在 acl 强制 sysdate pes ted sam 部分
The basic syntax for a SELECT statement is presented below. SELECT语句的基本语法如下。
In its simplest form, a SELECT statement must include the following:
In the syntax:
Note: Throughout this course, the words keyword, clause, and statement are used as follows:
——for example,SELECT and FROM are keywords.
——for example, SELECT emplovee_id, last_name , and so on
——for example, SELECT * FROM employees(SELECT *叫一个子句,FROM employees叫一个子句) 注意:习惯将关键字写成大写
Selecting All Columns:
Selecting Specific Columns:
Write SQL Statements
Column Heading Defaults: 默认的列标题(表的第一行):
Arithmetic expressions and NULL values in the SELECT statement SELECT语句中的算术表达式和空值
首先介绍显示表结构的命令 DESCRIBE command 描述命令:显示表结构 The structural metadata of a table may be obtained by querying the database for the list of columns that comprise it using the DESCRIBE command. It will list the used column names, their null property and data type. Syntax:
For example,
will display the EMPLOYEE table structure i.e. columns, their data types, precision and nullable property.
An arithmetic expression can be creaeted using the column names, operators and constant values to embed an expression in a SELECT statement. 算术表达式可以使用列名称、运算符和常量值嵌入一个SELECT语句中的表达创建。 The operator applicable to a column depends on column‘s data type. 适用于列的运算符依赖于列的数据类型。 For example, arithmetic operators will not fit for character literal values. 例如,算术运算符不适合字符面值。
For example,
The above query contains the arithmetic expression (sal * 12) to calculate annual salary of each employee. 上面的查询包含算术表达式(sal* 12)来计算每个雇员的年薪。
Arithmetic Expressions 算术表达式
你可能需要修改数据的显示方式,或想执行计算,通过使用算术表达式可以实现.
一个算术表达式可以包含列名、常量数字值和算术操作符.
Arithmetic operators 算术运算符 The slide lists the arithmetic operators that are available in SQL. You can use arithmetic operators in any clause of a SQL statement(except the FROM clause) 除了FROM子句,可在SELECT的任何其他子句中使用算术操作符. Note: With the DATE and TIMESTAMP data types, you can use the addition and subtraction. 对DATA和TIMESTAMP数据类型,只能使用+ -操作符.
Operators act upon the columns (known as operands) to result into a different result. In case of multiple operators in an expression, the order of evaulation is decided by the operator precedence. Here are the elementary rules of precedence -
Below table shows the precedence of the operators, in such cases. Precedence Level Operator Symbol Operation
Examine the below queries (a), (b), and (c)
Query (a) multiplies two numbers, while (b) shows addition of $1500 to salaries of all employees. Query (c) shows the addition of commission component to employee‘s salary. As per the precedence, first commission would be calculated on the salary, and then added to the salary.
注意,如上sal+200这个列是运算列,并不是真正存在的列,这里可以对这个列设定一个列别名.
Column Alias 列别名(可以改变列标题)
紧跟列名(也可以在列名和别名之间选择关键字)
如果别名列中有空格,或特殊字符,或区分大小写,这个时候这个列标题要用双引号引起来.
An alias is used to rename a column or an expression during display. The alias to a column or an expression appears as the heading in the output of a query. It is useful in providing a meaningful heading to long expressions in the SELECT query. By default, the alias appears in uppercase in the query output without spaces. To override this behavior, alias must be enclosed within double quotes to preserve the case and spaces in the alias name.
Concatenation operators 串联操作符|| A concatenation(串联) operator:
将列或字符串连接起来作为一个单独的列作为输出.
将列或字符串连接起来,创建一个组合列.
Concatenation operator can be used to join two string values or expressions in a SELECT query. The double vertical bar symbol is used as string concatenation operator. It is applicable only for character and string column values resulting into a new character expression. Example
The above query shows concatenation of two character literals values.
Literals 字面量/常量
常量可以是字符串,数字,日期.
日期和字符串常量必须用单引号引起来.
为返回的每一行打印一次.
单引号‘引起来的是字面量. 单引号为定界符.
Any hard coded value, which is not stored in database, in the SELECT clause, is known s Literal. It can be number, character, or date value. Character and date values must be enclosed within quotes. Consider the below SQL queries.examples of using literals of different data types in SQL queries. The query below uses two character literals to join them together.
The query below uses character literals to pretty print the employee‘s salary.
Quote Operator 引用运算符
The quote operator is used to specify the quotation mark delimiter of your own. You can chose a convenient delimiter, depedning on the data. 如下例子中,使用q操作符,中括号[]被作为字符串的定界符,里面的单引号‘就是普通的字符.
NULL空值
If a column doesn‘t has a definite value, it is considered as NULL. NULL value denotes unknown or unavailable. It is not zero for numeric values, not blank space for character values. Columns with NULL value can be selected in a SELECT query and can be the part of an arithmetic expression. Any arithmetic expression using NULL values results into NULL. For this reason, columns with NULL value must be handled differently by specifying their alternate values using Oracle supplied functions like NVL or NULLIF.
Duplicate Rows The default display of queries is all rows, including duplicate rows. DISTINCT Keyword:Suppresses(阻止) duplicates(重复) 这里可以选择多个列,但是他会取多个列的组合的重复,如列1的前三行为1,2,2;列2的前三行为1,3,4,会取出第一行,显示第二行和第三行. If the data is expected to have duplicate results, use DISTINCT keyword to eliminate duplicates and diplay only the unique results in the query output. Only the selected columns are validated for duplication and the rows will be logically eliminated from the query output. To be noted, the DISTINCT keyword must appear just after the SELECT clause. The simple query below demonstrates the use of DISTINCT to display unique department ids from EMPLOYEES table. DISTINCT后可以跟多个列,去掉多个列的组合完全一样的重复行.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1. Identify the capabilities of SELECT statement.
Answer: A, B. The SELECT statement can be used for selection, projection and joining. 2. Determine the capability of the SELECT statement demonstrated in the given query. SELECT e.ename, d.dname
Answer: A, C, D. Projection is including only the required columns in query, while Selection is selecting only the required data. Joining means combining two tables together through a connecting column. 3. Which of the following clause is used to suppress duplicates in a SELECT statement?
Answer: C, D. Duplicate data can be restricted with the use of DISTINCT or UNIQUE in the SELECT statement. 4. Chose the statements which correctly specify a rule to write a SQL statement
Answer: C.SQL statements are not case sensitive. 5. Determine the output of the below query - SELECT ‘5+7‘
Answer: B.Oracle treats the values within double quotes as string expressions. 6. Write a query to display employee details (Name, Department, Salary and Job) from EMP table.
Answer A.Select the required from the tables each separated by a comma. 7. Which of the below queries displays employees‘ name and new salary after the increment of 1000?
Answer: C. Basic arithmetic calculations can be done using the columns in SELECT statements. 8. Determine the output of the below query SELECT 36/2-5*10 FROM dual;
Answer: B. Multiplication and Division occur before addition and subtraction. 9. Determine the output of the below query SELECT (100-25)/15*(20-3) FROM dual;
Answer: D. Expression within the brackets are executed before the divisions and multiplications in the expression. 10. Chose the statements which correctly define a NULL value.
Answer: B, D.NULL is NO VALUE but neither same as zero nor as blank or space character. 11. Determine the output of the below query SELECT sal + NULL
Answer: B. Any arithmetic operation with NULL results in NULL. 12. Which of the below statements define column alias correctly?
Answer: A, D. Column Alias can be used to name an expression in the SELECT statement. 13. Specify the column alias NEWSAL for the expression containing salary in the below SQL query SELECT ename, job, sal + 100 FROM emp;
Answer: A, B.Use ‘AS‘ to signify new alias to a column expression. 14. Specify the column alias "New Salary" for the expression containing salary in the below SQL query SELECT ename, job, sal + 100 FROM emp;
Answer: B, D. Column alias with space and special characters must be enquoted within double quotes. 15. Which command is used to display the structure of a table?
Answer: C.DESCRIBE is used to show the table structure. 16. Predict the output when below statement is executed in SQL* Plus? DESC emp
Answer: C. DESCRIBE is used to show the table structure along with table columns, their data type and nullity 17. Which of the below statements are true about the DESCRIBE command?
Answer: B. 18. Which of the below alphanumeric characters are used to signify concatenation operator in SQL?
Answer: B.In SQL, concatenation operator is represented by two vertical bars (||). 19. Which of the below statements are correct about the usage of concatenation operator in SQL?
Answer: B, D. Concatenation operator joins two values as an expression. 20. Predict the output of the below query SELECT ename || NULL
Answer: A. Concatenation with NULL results into same value. 21. Predict the output of the below query SELECT 50 || 0001
Answer: C. The leading zeroes in the right operand of expression are ignored by Oracle. 22. You execute the below query SELECT e.ename||‘ departments‘s name is:‘|| d.dname And get the exception - ORA-01756: quoted string not properly terminated. Which of the following solutions can permanently resolve the problem?
Answer: B. The [q] operator is used to enquote character literals with a quote. 23. Which of the below SELECT statement shows the correct usage of [q] operator?
Answer: A 24. Which of the below SELECT statement is used to select all columns of EMP table?
Answer: C. The character ‘*‘ is used to select all the columns of the table. 25. Which of the below SQL query will display employee names, department, and annual salary?
Answer: C. Use numeric expressions in SELECT statement to perform basic arithmetic calculations.
|
SQL Fundamentals: Basic SELECT statement基本的select语句
标签:remove 存在 acl 强制 sysdate pes ted sam 部分
原文地址:http://www.cnblogs.com/scentpath/p/SELECT.html