Select Queries

From TRCCompSci - AQA Computer Science
Revision as of 13:16, 17 December 2016 by Admin (talk | contribs)
Jump to: navigation, search
  • Used for fetching information from an SQL database.
  • 'Outputs' the data once successfully selected
  • Allows selection from multiple tables but not multiple databases.

The Basic Construct

  SELECT `data` FROM `TABLE`
  WHERE `Condtion`
  ORDER BY `TABLE` ASC / DESC ;

Basic Example

 SELECT *
 FROM  Book
 WHERE price > 100.00
 ORDER BY title;

Remember the select can have * to select all fields, however questions will normally specify what fields to select. The from section should identify which table(s) to select the data from. The where section should include the criteria used to select the data, this could be a simple statement as above but remember you can also include other operators such as and, or, not, like, and so on.

Relational Databases & Select

 SELECT TutorGroup.Name, Student.Name
 FROM  TutorGroup, Student
 WHERE Student.ID = TutorGroup.StudentID
 AND Student.YearGroup = 12 AND Student.Gender = 'Male'
 ORDER BY TutorGroup.Name;