Writing SELECT queries for SQL databases
When you want to get results from a database you issue a select query. With this query you can limit the results based on certain criteria, sort it, and subsort it. You can do a lot with this including selecting data from multiple tables. This is done using joins which is a topic for another day.
First we are going to create a database called test with a people table.
The code to create our database is:
Now we need to create a table, and populate it with some data.Code:CREATE DATABASE test;
This SQL code creates a table with 3 fields in it. The id field is a unique field that we can use to refer to any records we want. Then we have a name field that is a variable character field. The age field is an integer field to hold a persons age.Code:CREATE TABLE `test`.`people` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `name` VARCHAR( 100 ) NOT NULL , `age` SMALLINT NOT NULL ) ENGINE = MYISAM ;
Since this is just a tutorial on select queries, I am going to give you some queries that we can use to demonstrate select queries.
Selecting all the dataCode:INSERT INTO `people` (`id`, `name`, `age`) VALUES (1, 'James', 16), (2, 'Joe', 5), (3, 'Jack', 9), (4, 'Bill', 15), (5, 'Topa', 93);
To select all data we simply use the * to mean select all. Then we name the table.
The basic syntax is:
This code is to select all the data:Code:SELECT field names | * FROM tableName;
The result set will look like this:Code:SELECT * FROM people;
Specifying the Columns
You can specify what columns you want to return also.
Example:
The resulting output is:Code:SELECT name, age FROM people;
Where Conditions
Using the WHERE clause you can limit the results that are returned. You might want to return all names of people who are less than 18? How about all names that start with J? All the math operators like less than, greater than, and so on you can use here.
Example:
The result is:Code:SELECT name, age FROM people WHERE age < 18
Notice how all the ages are less than 18?
How about all names that start with J? This is where pattern matching is important.
The result is:Code:SELECT name, age FROM people WHERE name LIKE 'J%'
Notice, how all the names start with J. That is what are where clause says.
Try this:
Notice, how the result is the same as above? The pattern matching does not depend on case.Code:SELECT name, age FROM people WHERE name LIKE 'j%'
ORDER BY and Sorting
When we use the ORDER BY clause we can sort our results by any field we want. Let us see how it works when we sort by age. You can also specify if you want to sort in ascending order or in descending order.
Output:Code:SELECT name, age FROM people ORDER BY age DESC;
The code said to select all the names and ages from people and to order it by age in descending order. So the first row is the largest age and so n.
We can easily sort by ascending order:
The output is:Code:SELECT name, age FROM people ORDER BY age ASC;
![]()
Very nice work, Chili5. +rep
Very nice +rep
Creating tables from queries is very useful I think. Thanks.
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks