top of page
Writer's pictureThe Tech Platform

Important SQL Queries you should know

SQL commands are the instructions used to communicate with a database to perform tasks, functions, and queries with data. SQL commands can be used to search the database and to do other functions like creating tables, adding data to tables, modifying data, and dropping tables.


Consider the below table:

image Source- W3School


1. SQL Query for Retrieving Tables

With the SELECT command, users can define the columns that they want to get in the query output. This command is also useful to get which column users want to see as the output table. The SELECT statement is applied to pick data from a table. The data retrieved is put in a result table, named the result-set. The output data is saved in a result table. This output table is also termed the result-set.


2. Query for Selecting Columns from a Table

These are perhaps the most useful SQL queries examples. In the example below, we are extracting the “CustomerID” column or attribute from the table “Customers”. The select statement is used to select data from the database.


Example:

SELECT CustomerID, CustomerName, Country FROM Customers;

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

SELECT * FROM EMPLOYEE;


3. Query Using a Constraint

This SQL query retrieves the specified attributes from the table on the constraint Country='Brazil'


Example:

SELECT CustomerID,CustomerName, PostalCode, Country 
FROM Customers
WHERE Country='Brazil';

Output:



4. Query 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. The order by clause is used to sort the data from the table. The order by clause should always be used in the last of the SQL query.


Example:

SELECT CUSTOMERID, CUSTOMERNAME, POSTALCODE 
FROM CUSTOMERS 
ORDER BY POSTALCODE;

Output:




5. Query Using ‘Group By’

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


Example:

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

Output:



6. Data Manipulation Using AVG, COUNT, SUM

The standard syntax of selecting attributes from a table is applicable to views as well.


AVG()
SELECT AVG(mark) FROM Student;

COUNT()
SELECT Count(Student_ID) FROM Student;

SUM()
SELECT Sum(Student_ID) FROM Student;


The Tech Platform

0 comments

Kommentare


bottom of page