SQL Select Query
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
An SQL Select is used to Query a Database table or a combination of database tables.
A Basic structure of an SQL Select Query looks like the below.
SELECT [column1, column2,... columnN] FROM [tablename] WHERE
column1 (operation) value1 (condition) columnN (operation) valueN
ORDER BY column1, columnN
GROUP BY column1, columnN
Breaking down the above structure,"SELECT column1, column2....columnN" - We are specifying a set of column names to be selected from the table.
FROM [tablename] - Here we are specifying the name of the table.
WHERE column1 (operation) value1 - The WHERE clause is optional. We are specifying filters on the TABLE.
Example: WHERE Customer_Name = "Jack Sparrow".
column1 refers to Customer_Name. operation refers to = and value1 refers to "Jack Sparrow".
condition is used to join two operations. Like AND or OR.
Example: WHERE Customer_Name = "Jack Sparrow" OR Customer_Name = "Third Street Saints"
Let us see an examples of SELECT Query which can make the above SYNTAX clear. Imagine we have a database TABLE called CUSTOMER.
If we want to query CUSTOMER to get all rows and columns, we can do that with a SELECT Query, as given below.
SELECT * FROM CUSTOMER
Result:
FIRSTNAME
LASTNAME
AGE
COUNTRY
Rambo
Robert
25
Belgium
Mugambo
Satraj
37
Norway
Nagashekar
Rao
47
India
Tom
Harry
27
Brazil
Dana
Laura
17
Australia
Rambo
Fox
57
New Zealand
Giselle
Chivvi
87
Japan
What if we only wanted FIRSTNAME AND COUNTRY from the CUSTOMER TABLE? Then we can query the table as shown below.
SELECT FIRSTNAME, COUNTRY FROM CUSTOMER
Result:
FIRSTNAME
COUNTRY
Rambo
Belgium
Mugambo
Norway
Nagashekar
India
Tom
Brazil
Dana
Australia
Rambo
New Zealand
Giselle
Japan
SELECT * from Customer ORDER BY AGE DESC
Result:
FIRSTNAME
LASTNAME
ADDRESS
COUNTRY
Giselle
Chivvi
87
Japan
Rambo
Fox
57
New Zealand
Nagashekar
Rao
47
India
Mugambo
Satraj
37
Norway
Tom
Harry
27
Brazil
Rambo
Robert
25
Belgium
Dana
Laura
17
Australia