SQL Fundamentals: Basic SELECT statement基本的select语句(控制操作的现实列)(FROM-SELECT)
SQL Fundamentals || Oracle SQL语言
Capabilities of the SELECT Statement(SELECT语句的功能)
Data retrieval from data base is done through appropriate and efficient use of SQL. Three concepts from relational theory encompass the capability of the SELECT statement: projection, selection, and joining.
- Projection(投影): A project operation selects only certain columns (fields) from a table. The result table has a subset of the available columns and can include anything from a single column to all available columns. (选择列的能力)
- Selection(选择): A select operation selects a subset of rows (records) in a table (relation) that satisfy a selection condition. The ability to select rows from out of complete result set is called Selection. It involves conditional filtering and data staging. The subset can range from no rows, if none of the rows satisfy the selection condition, to all rows in a table. (从表里选择所需要的行)
- Joinin连接): A join operation combines data from two or more tables based on one or more common column values. A join operation enables an information system user to process the relationships that exist between tables. The join operation is very powerful because it allows system users to investigate relationships among data elements that might not be anticipated at the time that a database is designed. (连接多个表)
Consider the above table structures. Fetching first_name name, department_id and salary for a single employee from EMPLOYEES table is Projection. Fetching employee details whose salary is less than 5000, from EMPLOYEES table is Selection. Fetching employee's first name, department name by joining EMPLOYEES and DEPARTMENTS is Joining.


- 控制操作的显示列:基本的SELECT语句
- 控制行:限定查询和排序显示
- 分组统计查询
简单查询语句语法:
SELECT [DISTINCT] * | 列名称 [AS] [列别名] , 列名称 [AS] [列别名] ,...
FROM 表名称 [表别名] ;
各个子句的执行顺序:
1、FROM-->
2、WHERE -->
3、GROUP BY(HAVING子句)-->
4、SELECT-->
5、ORDER BY-->
执行顺序:
- 第一步执行FROM字句:表示确定数据来源
- 第二部执行SELECT字句:确定要显示的数据列。
在整个简单查询字句,主要有2个子句:
|
SELECT字句: |
内容:
|
||||||
|
FROM字句: |
定义要使用的数据表(数据来源) |
其他查询操作:
|
消除重复数据 DISTINCT |
SELECT DISTINCT job FROM emp; |
|
四则运算 |
SELECT empno,ename,sal*12,sal/30 FROM emp; SELECT empno,ename,(sal+200)*12+5000 FROM emp ; |
|
为四则运算查询结果设置别名 |
SELECT empno 雇员编号,ename 雇员姓名,(sal+200)*12+5000 AS 年薪 FROM emp ; 别名中有空格或特殊字符或区分大小写,这时候用双引号括起来 |
|
常量 |
在SELECT子句中使用常量,为以上的查询增加一个货币的描述 SELECT empno AS 雇员编号 , ename AS 雇员姓名,(sal+200)*12+5000 AS 年薪 , '¥' AS 货币 FROM emp ; 日期和字符串常量用单引号括起来。 |
|
串联操作符|| |
使用“||”进行连接显示 SELECT '编号是:' || empno || '的雇员姓名是:' || ename || ',基本工资是:' || sal 雇员信息 FROM emp ; 在查询语句中出现的字符串,必须使用“'”括起来 |
|
引用运算符 []{}<> |
q'[']' SELECT ename,job,ename||q'['s job is]'||job as "Employees" FROM emp; |
- 简单查询是将一张表中的全部或部分列进行显示的操作;
- 简单查询中通过“*”表示查询全部的内容,也可以指定具体的列名称,显示具体列的内容;
- 在SQL中可以使用“+”、“-”、“*”、“/”四则运算,但是要注意运算符的优先级;
- 可以为一个显示的列进行别名的设置,这样以后显示时会将相应的列名称替换成别名显示;
- 通过“||”可以进行数据的连接,在查询语句中出现的字符串,必须使用“'”括起来。
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 描述命令:显示表结构 isplaying the Table Structure You can display the structure of a table by using the DESCRIBE command. The The 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
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
Concatenation operators 串联操作符|| A concatenation(串联) operator:
将列或字符串连接起来作为一个单独的列作为输出.
将列或字符串连接起来,创建一个组合列. Concatenation
The above query shows concatenation of two character literals values. Literals 字面量/常量
常量可以是字符串,数字,日期.
日期和字符串常量必须用单引号引起来.
为返回的每一行打印一次. 单引号'引起来的是字面量. 单引号为定界符. Any 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 如下例子中,使用q操作符,中括号[]被作为字符串的定界符,里面的单引号'就是普通的字符.
NULL空值
If Columns
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 simple query below demonstrates the use of DISTINCT to display unique department ids from EMPLOYEES table. DISTINCT后可以跟多个列,去掉多个列的组合完全一样的重复行.
使用DISTINCT 的方法COUNT函数和NVL函数的区别:
|
SQL Fundamentals: Basic SELECT statement基本的select语句(控制操作的现实列)(FROM-SELECT)的更多相关文章
- SQL Fundamentals || Oracle SQL语言
对于SQL语言,有两个组成部分: DML(data manipulation language) 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据 ...
- SQL Fundamentals: 子查询 || WHERE,HAVING,FROM,SELECT子句中使用子查询,WITH子句
SQL Fundamentals || Oracle SQL语言 子查询(基础) 1.认识子查询 2.WHERE子句中使用子查询 3.在HAVING子句中使用子查询 4.在FROM子句中使用子查询 5 ...
- Following a Select Statement Through Postgres Internals
This is the third of a series of posts based on a presentation I did at the Barcelona Ruby Conferenc ...
- Select Statement Syntax [AX 2012]
Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 ...
- SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解(SQL数据库和Oracle数据库的区别)
https://www.cnblogs.com/mq0036/p/4155136.html 我们经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个tabl ...
- 【转】PL/SQL编辑数据"这些查询结果不可更新,请包括ROWID或使用SELECT...FOR UPDATE获得可更新结果"处理
[转]PL/SQL编辑数据"这些查询结果不可更新,请包括ROWID或使用SELECT...FOR UPDATE获得可更新结果"处理 只要有人用了: select t.* from ...
- {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析
MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...
- A SELECT statement that assigns a value to a variable must ... (向变量赋值的 SELECT 语句不能与数据检索操作结合使用 )
A SELECT statement that assigns a value to a variable must ... (向变量赋值的 SELECT 语句不能与数据检索操作结合使用 ) 总结一句 ...
- Cannot resolve collation conflict between "Chinese_Taiwan_Stroke_CI_AS" and "Chinese_PRC_CI_AS" in UNION ALL operator occurring in SELECT statement column 1.
Cannot resolve collation conflict between . 解决方案: COLLATE Chinese_PRC_CI_AS 例子: SELECT A.Name FROM A ...
随机推荐
- Tomcat------启动时报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost].
启动报错信息: Failed to start component [StandardEngine[Catalina].StandardHost[localhost] 因此出现这种错误的原因可能有: ...
- 给一个由n-1个整数组成的未排序的序列,其元素都是1~n中的不同的整数。如何在线性时间复杂度内寻找序列中缺失的整数
思路分析:尼玛这不就是等差数列么.首先将该n-1个整数相加,得到sum,然后用(1+n)n/2减去sum,得到的差即为缺失的整数.因为1~n一共n个数,n个数的和为(1+n)n/2,而未排序数列的和为 ...
- 手机APP支付--整合支付宝支付控件
长话短说,本文根据支付宝官方说明文档,简单总结下,并且说明下开发过程碰到的问题以及该如何解决. 整合步骤: 1 登录商家服务网站,下载开发包,地址:https://b.alipay.com/order ...
- nodejs服务器部署教程三
安装mongodb数据库 如何在ubuntu上安装mongodb数据库,其实官方文档写的很清楚啦 sudo apt-key adv --keyserver hkp://keyserver.ubuntu ...
- RF使用ie浏览器访问页面,浏览器启动只显示This is the initial start page for the WebDriver server,页面访问失败
问题描述:启动ie浏览器后,页面显示如下: 问题定位: 1.IE页面缩放没有设置成100% 2.ie浏览器的安全模式设置是否都将“启动保护模式”勾选上 3.iedriver驱动版本号是否和seleni ...
- BootStrap Table将时间戳更改为日期格式
一.使用BootStrap Table遇到的问题: 1.MyBatis从数据库中取出的时间格式如下:2017-12-04 21:43:19.0,时间后面多了一个点零. 2.从BootStrap Tab ...
- postgresql 指令
(1)用户实用程序: createdb 创建一个新的PostgreSQL的数据库(和SQL语句:CREATE DATABASE 相同) createuser 创建一个新的PostgreSQL的用户(和 ...
- Unity3d OnApplicationPause与OnApplicationFocus
在手机游戏当中,会碰到“强制暂停”,如:锁屏.接电话或短信之类的.如果“强制暂停”时间过长,网络游戏有时得重新登录等事件. 而Unity3d,Android Plugins中的UnityPlayer. ...
- jquery 动态展示查询条件
<table class="queryTable" width="100%" > <tr> <td class="que ...
- hadoop自动提交脚本
自动提交到hadoop系统,然后调用wordcount的任务,并下载输出的文件. #!/bin/sh #从给定的路径获取文件列表,提交到hadoop系统,使用wordcount的功能统计单词数量 #e ...
