Introduction To Queries

Introduction

An SQL query can be one of the following:

  • Add/Remove table
  • Insert, modify, or delete rows or fields
  • Get specific results
  • Modify security information

General Rules of Syntax

Commands in SQL are not case sensitive.

Semicolon (;) tells the command-line SQL program
that your query is complete.

The most important elements are the keywords, such as SELECT, FROM and WHERE.

Examples will use the following table:

Name Age Occupation
John 20 Enginer
Chris 26 Chef
Bill 22 Student

The Building Blocks of Data Retrieval: SELECT and FROM

Example 1
Under the relational model, data is separated into sets that resemble a table structure. This table consists of individual data elements called columns or fields. A single set of a group of fields is known as a record or row.

For instance, to create a relational database consisting of employee data, you might start with a table called EMPLOYEE that contains the following pieces of information: Name, Age, and Occupation. These three pieces of data make up the fields in
the EMPLOYEE table.

The following SQL statement retrieves all the data from the above table.

SELECT * 
FROM EMPLOYEE

Changing the order of the Columns

Example 2

SELECT Age, Name, Occupation
FROM EMPLOYEE

will return the following table:

Age Name Occupation
20 John Enginer
26 Chris Chef
20 BIll Student

Example 3

SELECT Age
FROM EMPLOYEE

will return the following table:

Age
20
26
20

Example 4
In the previous table the Age 20 is repeated.

SELECT DISTINCT Age
FROM EMPLOYEE

will return the following table:

Age
20
26
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.