Summary:In this tutorial, you will learn how to MySQL SELECT statement to query data from database tables.

MySQL SELECT Statement Syntax

In order to retrieve data from MySQL database table you need to use MySQL SELECT statement. The following illustrates MySQL SELECT statment syntax:

1 SELECT column_name1,column_name2...
2 FROM tables
3 [WHERE conditions]
4 [GROUP BY group
5 [HAVING group_conditions]]
6 [ORDER BY sort_columns]
7 [LIMIT limits];

The MySQL SELECT statement has many optional elements that you can use. The order of FROM, WHERE, GROUP BY, HAVING, ORDER BY and LIMIT has to be in the sequence above.

To select all columns in a table you can use asterisk (*) notation instead of listing all column names in the MySQL SELECT statement. For example, if you need to query all the columns in offices table, you can use the following query:

1 SELECT * FROM employees

The MySQL SELECT statement also allows you to to view partial data of a table by listing columns’ name after the SELECT keyword.  This is called projection. For example if you need to view only first name, last name and job title of employee in the employees table, you can use the following query:

1 SELECT lastname,firstname,jobtitle
2 FROM employees

MySQL SELECT - query output 2

WHERE Clause

WHERE clause of the MySQL SELECT statement enables you to select particular rows which match its conditions or search criteria. You use WHERE clause to filter the records based on a certain conditions. For example, you can find the president of company by using the following query:

1 SELECT firstname,lastname,email
2 FROM employees
3 WHERE jobtitle="president"

MySQL SELECT WHERE - output query

DISTINCT

With DISTINCT keyword, you can eliminate the duplicate records from the SELECT statement. For example, to find how many job title of all employees in the employees table, you use DISTINCT keyword in SELECT statement as follows:

1 SELECT DISTINCT jobTitle FROM employees;

MySQL SELECT DISTINCT - query output

Sorting result with ORDER BY

The ORDER BY clause allows you to sort the result set on one or more columns in ascending or descending order. To sort the result set in ascending order you use ASC and in descending order you use DESC keywords. By default, the ORDER BY will sort the result set in ascending order. For example, to sort the name of employees by first name and job title, you can execute the following query:

1 SELECT firstname,lastname, jobtitle
2 FROM employees
3 ORDER BY firstname ASC,jobtitle DESC;

MySQL ORDER BY - query output

In this tutorial, you’ve learned about basic MySQL SELECT statement to retrieve data from one database table. You’ll learn more about each technique in details in later tutorials.

Related posts:

  1. Managing Database Index in MySQL
  2. Working with Database Table – Part I
  3. Managing Databases in MySQL
  4. Understanding MySQL TIMESTAMP
  5. Changing Table Structure Using MySQL ALTER TABLE

Leave your comment

You must be logged in to post a comment.


Link Exchange Spritual Place for Bhakti | bhaktiguru.com Business Franchise in India | businessfranchiseindia.com Free Directory Submission & Link Submission | ab-directory.com Big Dwarka Business Listing | bigdwarka.com Kolkata Online Business Listing | onlinekolkata.com Patna Online Business Listing | onlinepatna.com Dwarka Online Business Listing | onlinedwarka.com Bihar Online | First Bihar's Portal | bihar-online.com Big Bihar Business Listing | bigbihar.com Dynamic Website Development | dynamicwebsitedeveloper.com Noida Online Business Listing | onlinenoida.com SEO Service Delhi & India | seoservicedelhi.com SEO Experts Delhi & India | seoexpertsdelhi.com Big Patna Business Listing | bigpatna.com Bihar schools Listing Free Schools Ad | biharschools.com Creative Websoft Solutions | Web Development India | creativewebsoft.us Bulk SMS Providers India | bulksmsproviders.net Online cbse Guides | onlinecbseguides.com Big Kolkata Business Listing | bigkolkata.com Handicap Shaadi.com | handicapshaadi.com Handicap Marriage.com | handicapmarriage.com India Home Tutors.com | indiahometutors.com Indian Property Guides.com | indianpropertyguides.com Online Stocks Community.com | onlinestockscommunity.com Free Article Submission | ab-articles.com Secret Dating Online.com | secretdatingonline.com Private DatingOnline.com | privatedatingonline.com Indian businessguides.com | indianbusinessguides.com Software Engineer Delhi | Meenu Khanna Bollywood & Hot Images | hibdy.com Free Computer & Online PHP Training | php2php.com Free Ignou Project & Assignment | ignouinfo.com PHP Programmer India | Web Developer India | bageshsingh.com Creative Websoft Solutions | Web Development IndiaBlog Link Exchange http://www.php2php.com/tutorial-blog/http://bageshsingh.com/bagesh-blog/http://www.bhaktiguru.com/bhaktigurublog/http://www.hibdy.com/desi-masala-blog/