Top features of SELECT statement in SQL query



What is SELECT in SQL query? When executed, a SELECT statement always returns a table. The table may have only one column and no rows, but every SELECT statement returns its query results as a table.
  • Moreover, when a SELECT statement appears within another SELECT statement as a subquery, the inner SELECT statement returns a results table that serves as the input table for the outer (or main) SELECT statement.
If you are using interactive SQL (such as the window-oriented MS-SQL Server Query Analyzer or the command-line MS-SQL Server ISQL application), the SELECT statement displays its query results in tabular form on your computer screen.

If you are using a program (such as Visual Basic, C, or C++) to send a query (SELECT statement) to the DBMS, the DBMS will use a cursor to hold the tabular query results while it passes the rows of column values to your application program's (host) variables.

SELECT with WHERE clause

A SELECT statement with a WHERE clause will still return a table, but the results table will include only those rows from the input table(s) (list in the FROM clause) that satisfy the search condition in the WHERE clause. Thus, in the current example, the SELECT statement.


SELECT employee_id, first_name, last_name, total_sales
FROM employees
WHERE first_name IS NULL


will display the results:

employee_id first_name last_name total_sales
================================


In the above result table, we have only headers and no rows. Since there is no matching row.

SELECT statement in aggregate function

When you use an aggregate function in a SELECT statement to display a single value, the SELECT statement still returns its result as a table.

For example, based on the sample data in the current example's EMPLOYEES table, the SELECT statement

SELECT SUM(total_sales) FROM employees; 
will return the table: 
SUM(total_sales)
================ 
1072105.7900

Conclusion
  • The SELECT statement is that it always produces a table. Moreover, because they are always returned in a table.
  • you can store SELECT statement results back into the database as a table; combine the results of one SELECT statement with another to produce a larger, composite table
  • use the results of one SELECT statement as the target (or input) table for another SELECT statement.

Comments

Popular posts from this blog

Top myths for NULL value in SQL Queries