Generic SQL Commands

Preconditions:

=> Database has been installed according to https://www.hemelix.com/sql/sql-server-installation/

=> Database has been tested by running some of the example we have provided https://www.hemelix.com/sql/sql-data-modeling/

We need to make sure that SQL Server has been installed properly and also tested properly.

 

Aliasing:

SQL aliases are used to give a table, or a column in a table, a temporary name. These are often used to make column names more readable. It only exists for the duration of that query which is created with the AS keyword.

 

Alias for Columns Examples:

We can assign new column name  to existing column name.

Example, CustomerID  will be as ID and CustomerName  will be as Customer

SELECT CustomerID AS ID, CustomerName AS Customer
FROM Customers;

 

Alias for Tables Example:

Aliases are used to address database tables with a shorter or more meaningful name within an SQL query. The basic syntax of a table alias is as follows.

SELECT column1, column2....
FROM table_name AS alias_name

Example: 

SELECT C.ID, C.NAME, C.AGE, O.AMOUNT 
FROM CUSTOMERS AS C, ORDERS AS O
WHERE  C.ID = O.CUSTOMER_ID;

Find top 10 and bottom 10 items

//top 5
SELECT TOP 5 [NameId]
FROM [NameTable]
//bottom 5
SELECT TOP 5 [NameId]
FROM [NameTable] Order by Id desc

SQL Variables in Queries

To make query, we can use parameters.