Create SQL query - how it works
SQL queries are required to communicate with databases. This practical tip shows you how to create such a query.
SQL query: You should know that
- An SQL query is not difficult to understand, since it consists almost entirely of English words. It is important, however, that you write the SQL commands in the query in the correct order.
- SQL does not distinguish between upper and lower case. However, it is common to capitalize the SQL commands; database and table names are usually written in lower case.
- Always end the completed SQL command with a semicolon [;].
1. SQL command: SELECT
- The SELECT command is there to select columns from a table.
- If you want to select all columns, type "SELECT *", otherwise enter "SELECT" followed by the column names. You must separate the names of the columns with a comma [, ].
2nd command: FROM
- Use the FROM command to select the table in which the query is to be played.
- A small example: You want to select the columns "art-no", "article" and "price" from the table "gifts". The SQL command for this is "SELECT art-nr, article, price FROM gift article;". This SQL query already works, but can be expanded with additional commands.
Third SQL command: WHERE
- You can use the WHERE command to add a condition to your query.
- If you want to search for a text, you have to put it in single quotes.
- To continue the example from above: You only want to query the item "Wrapping paper". The SQL query now looks like this: "SELECT item no, item, price FROM gift item WHERE item = 'gift paper';".
Last but not least: ORDER BY
- If your query returns multiple results, you can sort them with ORDER BY.
- Specify which column to sort by, followed by "ASC" for ascending and "DESC" for descending order.
- Here, too, we build on the example from above: You have received several results for your query and want to sort them in descending order by price. The SQL query for this is: "SELECT item no, item, price FROM gift item WHERE item = 'gift paper' ORDER BY price DESC;".
In the next practical tip you will learn how to install and set up XAMPP.