Structured Query Language: Difference between revisions
imported>Oluwabusola Oladapo |
imported>Oluwabusola Oladapo (→Syntax) |
||
Line 6: | Line 6: | ||
The syntax of a simple SQL query: | The syntax of a simple SQL query: | ||
<pre> | <pre> | ||
SELECT { | SELECT {select-list} | ||
FROM { | FROM {table1, table2, ...} | ||
WHERE { | WHERE {predicates} | ||
GROUP BY {grouping-columns} | |||
HAVING predicates | |||
</pre> | </pre> | ||
All rows in the table specified in the FROM clause are retrieved. If more than one table is specified, the Cartesian product of the rows is produced. Next, the rows not satisfying the predicates provided in the WHERE clause eliminated. Then the GROUP BY clause groups the remaining rows according to the equal values in the specified grouping-columns. Next, groups not satisfying the predicates specified in the HAVING clause are eliminated. Finally, the columns specified in the select-list are returned. | |||
The WHERE,GROUP BY and HAVING clauses are optional. | |||
== Examples == | |||
Here is a simple example that returns details from a table called 'students', only if the row has a GPA higher than 3: | Here is a simple example that returns details from a table called 'students', only if the row has a GPA higher than 3: | ||
<code> | <code> |
Revision as of 01:06, 23 July 2008
![](http://s9.addthis.com/button1-share.gif)
Structured Query Language (SQL) is a simple language designed for querying and managing Relational Database Management Systems (RDBMS). RDBMSs organize data using tables, where each table has named column(s). Each column has one datatype, and data is stored as rows intersecting with these columns and therefore satisfy the datatype of the corresponding column. For example, a student table could have two columns, student_id and student_name. If I want to insert data into this table, I would insert a row of two values, a student_id e.g. 001 and a student_name e.g. Claire. It is this concept of tables that is the basis of SQL, which has become the de facto standard adopted by most database management systems vendors. However, most vendors provide an extension to SQL to serve various functions ranging from the integration of procedural constructs to exception handling.
Syntax
The syntax of a simple SQL query:
SELECT {select-list} FROM {table1, table2, ...} WHERE {predicates} GROUP BY {grouping-columns} HAVING predicates
All rows in the table specified in the FROM clause are retrieved. If more than one table is specified, the Cartesian product of the rows is produced. Next, the rows not satisfying the predicates provided in the WHERE clause eliminated. Then the GROUP BY clause groups the remaining rows according to the equal values in the specified grouping-columns. Next, groups not satisfying the predicates specified in the HAVING clause are eliminated. Finally, the columns specified in the select-list are returned.
The WHERE,GROUP BY and HAVING clauses are optional.
Examples
Here is a simple example that returns details from a table called 'students', only if the row has a GPA higher than 3:
SELECT name, rollnum, totalmarks
FROM students
WHERE gpa > 3;
The columns 'name', 'rollnum' and 'totalmarks' must be present in the 'students' table, although the table could have other columns. The result is a set of rows that satisfy the condition (gpa > 3).
Queries can be nested in order to give more specific results as desired.