sql


Structured Query Language (SQL)

Structured Query Language is a standard Database language which is used to create, maintain and retrieve the relational database.

What is Relational Database?

Relational database means the data is stored as well as retrieved in the form of relations (tables).



  Query for Retrieving Tables

This query can be run to retrieve the list of tables present in a database where the database is “My_Schema”.

SELECT * FROM My_Schema.Tables;

SELECT * FROM student;



 Query for Selecting Columns from a Table

This is perhaps the most widely used of SQL queries examples. In the example below, we are extracting the “Student_ID” column or attribute from the table “STUDENT”.

                SELECT Student_ID FROM STUDENT;

If you want to display all the attributes from a particular table, this is the right query to use:

                SELECT * FROM STUDENT;


 Query for Outputting Data Using a Constraint


This SQL query retrieves the specified attributes from the table on the constraint Employee ID =0000

                SELECT EMP_ID, NAME FROM EMPLOYEE_TBL WHERE EMP_ID = '0000';


 Query for Outputting Sorted Data Using Order By



This query orders the results with respect to the attribute which is referenced to using “Order By” – so for example, if that attribute is an integer data type, then the result would either be sorted in ascending or descending order; likewise, if the data type is a String then the result would be ordered in alphabetical order.



                SELECT EMP_ID, LAST_NAME FROM EMPLOYEE WHERE CITY = 'Seattle' ORDER BY EMP_ID;



The ordering of the result can also be set manually, using “asc ” for ascending and “desc”  for descending.



SELECT EMP_ID, LAST_NAME FROM EMPLOYEE_TBL WHERE CITY = 'INDIANAPOLIS' ORDER BY EMP_ID ASC;



SELECT EMP_ID, LAST_NAME FROM EMPLOYEE_TBL WHERE CITY = 'INDIANAPOLIS' ORDER BY EMP_ID DESC;

Query for Outputting Sorted Data Using ‘Group By’

The ‘Group By’ property groups the resulting data according to the specified attribute.



SELECT Name, Age FROM Patients WHERE Age > 40 GROUP BY Age ORDER BY Name;




Data Manipulation Using COUNT

This query displays the total number of customers by counting each customer ID. In addition, it groups the results according to the country of each customer.


SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country;











Popular Posts