SQL Commands-Select, Distinct

 Select and Distinct Command in SQL


The Select command

The select command of SQL let you make queries on the database. A query is command that is given to produce certain specified information from the database table (s).

There are various ways and combinations, A select statement can be used into.it can be used to retrieve a subset of rows or columns from one or more tables.

In its simplest form, select statement is used as:

Select column_name1, column_name2,...
from table_name;

For example, if you want to view only the information of two columns empno and  empname of a table emp, you may write your query as :

Select empno, empame from emp;

If you want to see the information of columns empno , job and salary from the table employee, you will write:

Select empno, job, sal from emp;

If you want to see the entire table that is every column of a table, you need not to give a complete list of columns, the asterisk(*) can be substituted from the complete list of columns as follows.

Select * from emp;

Eliminating redundant data (With Keyword DISTINCT)

By default, that is selected from all the rows of the table even if the data is appearing in the result gets duplicated. The distinct keyword eliminate duplicate rows from the result of a select element.
For example, if the suppliers table stores the name and cities of the suppliers and we want to see the cities where suppliers belongs to. The table may consists of more than one supplier belong to the same city but the result of the query should not contain duplicate City names to do so, we shall write:-

Select distinct City from suppliers;

In this output, there would be no duplicate rows. whenever distinct is used only one null value is returned in the results, no matter how many Nan values are encountered.
distinct, in fact, applies the entire output row, not a specified field. The distinct keyword can be specified only once in a given select clause.if clause select multiple fields, distinct eliminates rows where all the selected fields are identical. Rows in which some values are the same and some different will be retained.

Comments