SQL Statements
SQL Statements
Here i am explaining some useful sql Statements of queries
· SQL SELECT Statement
· SQL SELECT DISTINCT Statement
· SQL WHERE Clause or Condition
· SQL AND Operator
Firstly i am creating the common table named Grahak for use in some of the sql statements which are explaining in this article
Table named Grahak with column and rows with Values
CustomerID |
CustomerName |
Contactnumber |
Address |
City |
PostalCode |
Country |
1 |
Ram |
9871234563 |
#47 Sector 22 |
Chandigah |
160002 |
Japan |
2 |
Sham |
9876543216 |
#4110 Sector 122 |
Mohali |
140002 |
India |
3 |
Shivam |
3278955152 |
#5 Sector 88 |
Gurgaon |
180002 |
UAE |
4 |
Ravi |
2277894562 |
#5 Sector 48 |
Badlapur |
220002 |
North korea |
Different SQL Statements
1. SQL SELECT Statement
The SELECT
statement is used to fetch data from
the database.
Syntax
SELECT column_first, column_second, column_third, ...and so on
FROM table_name;
Here, column_first, column_second, column_third, ... and so on are the column names of the table you want to fetch data from.
The table_name presents the name of the table from where you want to fetch data.
Example
Fetch data from the Grahak table:
SELECT CustomerName, City FROM Grahak;
Below is a selection from the Grahak table used in the example:-
Select ALL columns
If you want to fetch all
columns, without mentioning all column name, you can use the SELECT *
syntax:
Example
Fetch all the columns from the Customers table:
SELECT * FROM Grahak;
2. SQL SELECT DISTINCT Statement
The SELECT DISTINCT
statement is used to fetch only
distinct (different) from the table
Example
Select all the different countries from the "Grahak" table:
SELECT DISTINCT Country FROM Grahak;
Syntax
SELECT DISTINCT column_first, column_second, column_third, ...and so on
FROM table_name;
Count Distinct
By using the DISTINCT
keyword in a function called COUNT
, we can fetch the number of different
countries from the Grahak table.
Example
SELECT COUNT(DISTINCT Country) FROM Grahak;
3. SQL WHERE Clause or Condition
The WHERE
clause or condition is used to filter
records from the table.
It is used to fetch only those records from the table that accomplish a specific condition.
Example
Select all customers from India:
SELECT
* FROM
Grahak
WHERE Country='India';
SQL ORDER BY
The ORDER BY
keyword is used to fetch the data in
ascending or descending order from the table.
Example
Sort the products by price:
SELECT
* FROM
items
ORDER BY Price;
Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Below is a selection from the items table used in the examples
ProductID |
ProductName |
SupplierID |
CategoryID |
Unit |
Price |
1 |
Apple |
1 |
1 |
50 |
18 |
2 |
Orange |
1 |
1 |
45 |
19 |
3 |
Banana |
1 |
2 |
88 |
10 |
4 |
Water Melon |
2 |
2 |
95 |
22 |
DESC
The ORDER BY
keyword fetch the records with
sorting in ascending order by default.
For sort the records of the table in descending order, use the DESC
keyword.
Example
Sort the items from the table as highest to lowest price:
SELECT
* FROM
items
ORDER BY Price DESC;
Order Alphabetically
For string values the ORDER BY
keyword will order alphabetically:
Example
Sort the items alphabetically by ProductName:
SELECT
* FROM
Products
ORDER BY ProductName;
Alphabetically DESC
To sort the table reverse
alphabetically, use the DESC
keyword:
Example
Sort the products by ProductName in reverse order:
SELECT
* FROM
items
ORDER BY ProductName DESC;
ORDER BY Several Columns
The following SQL statement selects all customers from the "items" table, sorted by the "Country" and the "CustomerName" column. This means that it orders by Country, but if some rows have the same Country, it orders them by CustomerName:
Example
SELECT
* FROM
items
ORDER BY Country, CustomerName;
Using Both ASC and DESC
The following SQL statement selects all customers from the "Customers" table, sorted ascending by the "Country" and descending by the "CustomerName" column:
Example
SELECT
* FROM
items
ORDER BY Country ASC, CustomerName DESC;
4. SQL AND Operator
The WHERE
clause can contain one or many AND
operators.
The AND
operator is used to filter records
based on more than one condition, like if you want to return all Grahak from Spain that starts with the letter 'G':
Example
Select all customers from Spain that starts with the letter 'G':
SELECT
*
FROM Grahak
WHERE Country = 'India' AND CustomerName LIKE 'G%';
Syntax
SELECT column_first, column_second, column_third, ...and
so on
FROM table_name
WHERE condition_first AND condition_second AND condition_third
... and so on;
To fetch the data from the table using And operatorall conditions must be true
The following SQL statement
selects all fields from Grahak
where Country
is "India" AND City
is "Chandigarh" AND CustomerID
is higher than 50:
Example
SELECT
* FROM
Grahak
WHERE Country = 'India'
AND City = 'Chandigarh'
AND CustomerID > 50;
Comments
Post a Comment