DATA with BARAA

SQL Select

SQL SELECT

In this tutorial, you will learn how to retrieve your data from database tables using the SELECT clause.

The  SELECT  statement or we call it Query, is used to retrieve all rows from a table or retrieve only those rows that satisfy a certain condition(s). 

Syntax

The basic syntax of the SELECT statement to select all columns, can be given with:

				
					SELECT * 
FROM table_name
				
			

The asterisk (*) or we call it sometimes star means everything. Once you use it after SELECT, you tell the database that you want all columns from the table.

To include all columns use:

				
					SELECT colum_names
FROM table_name
				
			

Insead of using asterisk (*) after SELECT, we will be now listing the name of columns of a table whose you want to retrive.

.

Examples

To understand the SELECT statement in a better way, let’s look at the following customers table in our tutorial database:

Customers
customer_id first_name last_name country score
1 Maria Cramer Germany 350
2 John Steel USA 900
3 Georg Pipps UK 750
4 Martin Müller Germany 500
5 Peter Franken USA NULL

Now, let’s check out some examples that demonstrate how it actually works.

We have the following task be to solve using SQL statements

Retrieve ALL data and fields from customers

The following Query will returns all the rows from customers table.

				
					SELECT * 
FROM customers
				
			

After executing the above query, you’ll get the result set something like this:

customer_id first_name last_name country score
1 Maria Cramer Germany 350
2 John Steel USA 900
3 Georg Pipps UK 750
4 Martin Müller Germany 500
5 Peter Franken USA NULL

As you can see the output contains everything the whole customers tables including all rows and columns.

SELECT * helps you to examine the content of table that you are not familiar with. But be careful using it specially with big tables because database will retrieve everything which costs a lot of data movement across the network and might slow down the application

Examples

Let’s say we are only interested in getting only specific columns from a table, then we could use the following  syntax of the SELECT statement:

We have the following task to be solve using SQL statements

Retrieve only first name and country of all customers

In the task we don’t require all the data, we need only specific columns. The following SQL statement selects only first_name and country from table customers.

				
					SELECT 
    first_name,
    country
FROM customers
				
			

After executing the above query, you’ll get the result set something like this:

first_name country
Maria Germany
John USA
Georg UK
Martin Germany
Peter USA

As you can see the result contains only the columns that we specified after SELECT.

.
Share it !
Exit mobile version