解释 DBMS 中的 Select 命令
Select 命令用于通过 SQL 连接从表、视图或一组表、视图中获取一组记录中的数据。
使用 SQL 语句检索数据可以通过使用不同的谓词来完成,例如 −
- Where
- Group By
- Having
- Order By
最简单的 select 语句示例,用户想要检索表中的所有记录,可以使用 '*' 来执行。
首先,让我们创建并插入数据表,然后再使用 select 命令 − 检索数据。
步骤 1
create table student(name char(30), regno number(10), branch char(20), age char(10));
输出结果如下:表已创建
步骤 2
insert into student values(‘hari’,100,’CSE’, 15);
输出结果如下:已创建 1 行
步骤 3
insert into student values(‘pinky’,101,’CSE’,17);
输出如下:创建 1 行
步骤 4
insert into student values(‘bob’,102,’CSE’,14);
输出如下:创建 1 行
步骤 5
insert into student values(‘bhanu’,103,’CSE’,18);
输出如下:创建 1 行
步骤 6
Select * from student;
输出如下 −
| Name | Regno | Branch | Age |
|---|---|---|---|
| Hari | 100 | CSE | 15 |
| Pinky | 101 | CSE | 17 |
| Bob | 102 | CSE | 14 |
| Bhanu | 103 | CSE | 18 |
它显示了学生表中的所有记录,如上所示。
Where 子句
Where 子句与数据操作语言 (DML) 语句一起使用,用于检查行中是否满足某个条件。
示例 1
下面的查询显示年龄在 15 到 20 岁之间的学生记录。
SELECT * FROM student where age>15 and age<20; (OR) SELECT * FROM student where age between 15 and 20;
输出如下 −
| Name | Regno | Branch | Age |
|---|---|---|---|
| Pinky | 101 | CSE | 17 |
| Bhanu | 103 | CSE | 18 |
示例 2
考虑另一个示例以了解有关 where 子句 − 的更多信息
SELECT *FROM student where name like B%;
上述查询检索所有以字符 'B' 开头的姓名。
| Name | Regno | Branch | Age |
|---|---|---|---|
| Bob | 102 | CSE | 14 |
| Bhanu | 103 | CSE | 18 |
Group By 子句
结构化查询语言 (SQL) 中的 Group By 子句语句用于聚合,这意味着返回的结果基于列聚合。
示例
SELECT regno, sum(marks) FROM student
WHERE class=5
GROUP BY regno
Having 子句
SQL 中的 having 语句确保 SQL SELECT 语句仅返回聚合值符合指定条件的行。
示例
SELECT regno, sum(marks) from student WHERE admissionDate='01-Mar-2021'
GROUP BY regno
HAVING sum(marks)>600
Order By 子句
SQL 中的 order by 子句用于设置输出的字母顺序、大小顺序和日期顺序。它可以与 'asc' 或 'desc' 子句一起使用,以指定结果是升序还是降序。
注意:如果我们没有提到 'ans' 或 'desc',则 order by 子句默认采用升序。
示例
SELECT firstname, lastname from student ORDER BY firstname ASC;

