|
Custom Search
| |
|
The Basic SELECT Statement To retrieve data from a table, we use SQL (Structured Query Language) SELECT statement. Read Prerequisites for this tutorial and practices if you haven't done so. What you can do with SELECT statement With a SELECT statement, you can retrieve information from a database table in the format you want. Basically, you can control what rows and columns for the data retrieval.For rows, you can:
For columns, you can:
Using a simple SELECT statement Here is what a simple SELECT statement looks like: SELECT column1, column2, column3... FROM tablename
Note that SELECT and FROM can be called keyword or clause. In other words, keyword and clause can be used interchangeably. Putting two or more clauses together in a SQL forms a SQL statement. Column names must not use MySQL reserved words. If you do, escape them by using backticks ``. Most of the queries in the tutorials need Northwind MySQL database, you can download the database script on this page. Practice #1: select all columns, all rows SELECT * SELECT * SELECT CategoryID, CategoryName, Description, Picture SELECT CategoryID, CategoryName, Description, Picture Four select statements are listed above. They retrieve all rows and all columns from categories table. Asterisk (*) means all columns. We can also explicitly list all column names (CategoryID, CategoryName, Description, Picture) in the statement. It's equivalent to using Asterisk. Back quotes are used to enclose a table name. It's not necessary to use back quotes around a table name unless the table name has space(s) in it.
Query result set - 8 rows returned: Practice #2: select specific columns, all rows and add comments to the SQL query -- Only select data from two columns This example selects data for all rows but only CategoryID and CategoryName column. Columns are displayed in the same order you list them in the SQL statement.
Query result set - 8 rows returned: You can add comments to your MySQL code. There are three ways to add comments:
Some rules to follow when writing SELECT statement:
Other tutorials in this category 1. Using Column Alias in SELECT Statement |
Copyright © 2024 GeeksEngine.com. All Rights Reserved. This website is hosted by HostGator. No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it. |
Home | Feedback | Terms of Use | Privacy Policy |