MS SQL SERVER & MYSQL SELECT database with DISTINCT, WHERE & ORDER BY Clause
The SELECT statement in SQL syntax is used to retrieve data from the tables that exist in the database that we want to retrieve. Displays this result in tabular format.
Syntax:
SELECT column_name FROM table_name;
The semicolon is used at the end of the query just to show that our query is completed, for example, If you write two or more select queries than each query is separated by a semicolon. And FROM is used to connect to the respective table we want to deal with. The syntax of MS SQL SERVER and MYSQL SELECT database is the same.
Demo DataBase:
1ST:
2ND:
Cases of SELECT Query:
Retrieve all the data from a table:
Practical Example:
MS SQL SERVER retrieve all data from a table |
Retrieve One Column from a table:
Write a statement that displays DNAME from DPT.
SELECT DNAME FROM DPT;
Practical Example:
MYSQL retrieve data |
SQL SERVER retrieves a single column |
Retrieve multiple Columns from a table:
Write a statement that displays the columns DPTNO and DNAME from DPT.
SELECT DPTNO, DNAME FROM DPT;
Practical Example:
MYSQL WORKBENCH fetch multiple columns |
SQL SERVER fetch multiple columns |
Retrieve data from multiple tables:
[NOTE]
Practical Example:
CLAUSES in SQL:
With Distinct Clause
The distinct clause is used to eliminate duplicate rows from the result of a Statement. If Distinct is not used, all rows are returned including duplicates.
Write a query to display all distinct LOC from DPT.
SELECT DISTINCT LOC FROM DPT;
Practical Example:
With Where Clause
Where clause is used to retrieve data from a table conditionally. It can appear only after From clause.
Example:
Write a query that displays records of departments located in NEW YORK from 1st table.
SELECT * FROM DPT WHERE Loc = 'NEW YORK ' ;
Practical Example:
MYSQL WHERE clause |
SQL SERVER WHERE clause |
[NOTE]
With Order BY Clause
The Order BY clause is used to sort the row. The process of arranging data or records in a sequence is called SORTING. A sort can be ascending or descending. Structured query language uses ASC keyword to specify ascending sort and DESC keyword for descending sort, and by default, data is arranged in ascending order. Order BY must be the last clause in the Select statement.
Example:
Display records of departments located in NEW YORK from DPT table in descending order by DPTNO.
SELECT * FROM DPT WHERE Loc = 'NEW YORK ' ORDER BY DPTNO DESC ;
Practical Example
MYSQL WORKBENCH ORDER BY |
SQL SERVER ORDER BY |
Really appreciatable initiative. Easy way to the top
ReplyDeletefine work. good examples to learn SQl.
ReplyDeleteMasha Allah great work man , its very informative .
ReplyDelete