SQL:Data Query Language

From virtualMV2015wiki
Jump to: navigation, search
 Home  MySQL <

Introduction

The Data Query Language (DQL)is used to display the data in a table or tables. The following SQL Commands are available:

  • SELECT
Icon Objective.png

Objective

By the end of this page you should be :

  • familiar with the basic DQL command: SELECT
  • Understand examples of summarising the data

DDL Example

In the heights database the following DQL commands were sent to MySQL:

SELECT * FROM tblPeople;
 
SELECT * FROM tblPeople ORDER BY PerHeightmm; 
 
SELECT * FROM tblPeople WHERE PerHeightmm > 1400;

SELECT

Backus-Naur Form

SELECT
    [ALL | DISTINCT | DISTINCTROW ]
      [HIGH_PRIORITY]
      [STRAIGHT_JOIN]
      [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
      [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr ...]
    [FROM table_references
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_condition]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [INTO OUTFILE 'file_name' export_options
      | INTO DUMPFILE 'file_name'
      | INTO var_name [, var_name]]
    [FOR UPDATE | LOCK IN SHARE MODE]]

SELECT Syntax[1]


Icon Notes.png

Notes

The order you enter the commands is important. So SELECT .. FROM .. WHERE .. ORDER BY is a correct sequence.

Examples

SELECT * FROM tblPeople;

Thsi lists all the people and all the fields in the People table

SELECT * FROM tblPeople ORDER BY PerHeightmm;

This selects all the fields (indicated by *) from the People table (tblPeople) and sorts them (ORDER BY) their heights.

SELECT * FROM tblPeople WHERE PerHeightmm > 1400;

Icon References.png References

  1. 12.2.8 SELECT Syntax (2010) In MySQL ::MySQL 5.0 Reference Manual, Oracle. Retrieved September 10, 2010 from http://dev.mysql.com/doc/refman/5.0/en/select.html

SQL:Data Query Language. (2017). In virtualMV's ( Michael Verhaart ) wiki. Retrieved December 18, 2017, from http://www.virtualmv.com/wiki/index.php?title=SQL%3AData_Query_Language    (zotero)