Select syntax General structure:
1 SELECT [all| DISTINCT] <Target column Expression> [aliases] [, < target column expression > [alias]]...2 from <Table name or view name> [aliases] [,< table name or view name > [Alias]]...3 [WHERE < conditional expressions >]4 [GROUP by < column name 1> [having < conditional expression >]]5 [ORDER by < column name 2>[asc| DESC]
It is known that the SELECT statement consists of several components or clauses. There are some clauses that are indispensable (select clauses), but some are not used.
Clause name |
Purpose of Use |
Select |
Determine which columns should be included in the result set |
From |
The table that indicates the data to be extracted, and how the tables are connected |
where |
Filter unwanted data |
GROUP BY |
Used to group rows with the same column values |
Having |
Filter out unwanted groups (must be used with the GROUP BY clause) |
ORDER BY |
Sort the rows in the final result set by one or more columns |
0.select clause
As the first part of the SELECT statement, select is executed last because you need to know all the columns that might be included in the result set.
Select * from Department;
Here * represents the wildcard character, which means selecting all the columns from the department table, which is equivalent to writing out all the attributes in the department, and if you want to display only the required attribute columns, write them directly. Of course, you can also modify the SELECT clause.
- Characters, such as numbers or strings
- expressions, such as dept_id * 3
- Call the built-in function, such as upper (name)
- User-defined Functions
By querying, the default property name for each column is returned, and you can also customize the name, which is the alias, which can be aliased directly after the property name, or the property name as alias.
By default, the SELECT statement does not remove duplicate rows, and then adds distinct if not required, and all (the default) if all is displayed. However, it is important to note that using DISTINCT, the database sorts the results of the query, which is a waste of time and efficiency for larger result sets, so do not use them arbitrarily.
1.frome clause
The FROM clause defines the tables used in the query and how the tables are connected.
2.where clause
The WHERE clause is used to filter out unwanted rows in the result set.
The where is followed by a conditional expression, and the tuple that satisfies the criteria will be selected. Such as:
Select emp_id, fname, lname, start_date, title from Employee where = ' Head Teller ';
As you can see, only the tuple with title Head teller is listed.
Conditional expressions are varied, and are only used, equal conditions, and other expressions are explained later.
3.group by and HAVING clause
The GROUP BY clause is used to group data based on column values. The HAVING clause is used in conjunction with the GROUP BY clause to select a tuple that satisfies the criteria.
Select Count (e.emp_id) num_employees from Inner Join on = e.dept_id Group by D.name having Count > 2;
4.order BY clause
The ORDER BY clause is used to sort the original column data in the result set or the result of an expression computed from the column data.
If the query is in general case
Select open_emp_id, Product_cd from Account;
Visible results have no order, if the ORDER BY clause is used
Select open_emp_id, Product_cd from Account Order by open_emp_id;
As a result, the results are sorted in ascending order of open_emp_id (default), followed by Desc if you want to sort in descending order. You can also sort by multiple attributes in ascending or descending order.
Getting started with SQL Getting started with queries