SQL Query Tutorial part 1

Noor ul Huda
This post is all about SQL queries. For beginners and students, it will be helpful.

1- How to select all records/columns from table?

For above statement, just open query editior and write query like:

SELECT *
FROM Customers;


In this query, SELECT and FROM are SQL keywords. These are reserved words and have special meaning. SELECT keyword used for selecting columns from table(s). As I want to fetch all columns from table,  used * for this scenario. After Running this query, you 'll get that result:



2- How to select specific columns from table?

If any one want to just fetching records from specific columns then just specify names of columns of that table like i do.

SELECT customerNumber AS C_ID, customerName AS C_NAME, 
phone AS C_PHONE, city AS C_CITY, state AS C_STATE
FROM customers;

In above query, i used AS keyword which actually used as alias or can say renaming column name for ease of  querying and displaying records. In this query i select some columns from customers table.
This query will display records like this:


3- How to filter records from table?

If any one want to fetch records from table just by giving some value of any column
There is WHERE keyword used in SQL for filtering records of table. As i write query for this statement.

SELECT *
FROM customers
WHERE customerNumber=103;


In above query, i used WHERE keyword which actually filter records of  customers table.
In query after WHERE keyword, there is column name customerNumber which matches value 103 in column and return results. This query will display records like this:


4 Concatenating Column Values.

If there is scenario to fetch records but first name and last name columns values must come in single column value. Then, we have to concatenate values of these columns. As I do in this query:

SELECT CONCAT(contactFirstName, contactLastName) As "Name"
FROM customers; 
 

In above query, i used CONCAT keyword, it is built in function of SQL. Its function is to merge multiple column values in single column. As I concatenate first name and last name of customer in single column
After running this query you will get result like this:

5 Displaying Distinct Rows.

If we want to display unique records, no duplication of records, then we used DISTINCT keyword before column name and query will display unique records of that column. As I do in this query:

SELECT DISTINCT state
FROM customers; 

In above query, i used DISTINCT keyword which returns distinct records, no duplication in record values.
this query will display records like :

 
 You can download database sql file for practice. Enjoy Learning and give me feedback of this tutorial.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !