SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL what-are-single-row-and-multiple-row-subqueries">What are single row and multiple row subqueries?

SQL Aliases

SQL Aliases:

These are used to give a temporary name for a column or table. These are used to make tables or columns more readable.

  • These are used to rename the table or column temporarily. But it does not change in the original database.
  • Aliases are used when the table names or column names  are big and they are not readable
  • Alias is used with the “AS” Keyword.
  • It exists only for the duration of a particular query.

Alias Column Syntax:

select columnname AS aliasname from tablename;

Alias Table Syntax:

select columnname from tablename AS aliasname;

Alias Column Example:

Example:

Student Table

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Changing one column name by Alias:

select sname AS studentname from student;

Output:

Student Table

studentname
Abhinav
Ramya
Preetham
Nethranand
Naveen
Harshitha
Bindhu
Nandhini
Hashish
Rahul
Select sid AS studentid from Student;

Output:

Student Table:

studentid
1
2
3
4
5
6
7
8
9
10

Changing two column names by Alias:

Select sid AS studentid, sname AS studentname from student;

Output:

Student Table

studentidstudentname
1Abhinav
2Ramya
3Preetham
4Nethranand
5Naveen
6Harshitha
7 8Bindhu Nandhini
9Hashish
10Rahul
Select sid AS studentid, sage AS studentage from student;

Output:

Student Table

studentidstudentage
122
224
321
421
523
622
726
823
922
1021

Changing three column names by Alias:

Select sid AS studentid, sname AS studentname , sage AS studentage from student;

Output:

Student Table

studentidstudentnamestudentage
1Abhinav22
2Ramya24
3Preetham21
4Nethranand21
5Naveen23
6Harshita22
7Bindu26
8Nandhini23
9Hashish22
10Rahul21

Using Multiple column names as one column name by Alias:

Method I: select * from Student Table AS Student Data;

SELECT sname , sid + ', ' + sage + ', ' + Phonenumber AS studentdetails
FROM student;

Output:

Student Table

snamestudentdetails
Abhinav1, 22, Male, 9895678909
Ramya2, 24, Female, 6687654634
Preetham3, 21, Male, 9867546453
Nethranand4, 21, Male, 7675643423
Naveen5, 23, Male, 6567784532
Harshita6, 22, Female, 9867546231
Bindu7, 26, Female, 6563412768
Nandhini8, 23, Female, 6785674839
Hashish9, 22, Male, 9453215052
Rahul10, 21, Male, 9998989898

Method II:

Using concat operator

SELECT sname, CONCAT(sid, ', ', sage, ', ', sgender, ‘,’ Phonenumber) AS studentdetails
FROM Customers;

Output:

Student Table

snamestudentdetails
Abhinav1, 22, Male, 9895678909
Ramya2, 24, Female, 6687654634
Preetham3, 21, Male, 9867546453
Nethranand4, 21, Male, 7675643423
Naveen5, 23, Male, 6567784532
Harshita6, 22, Female, 9867546231
Bindu7, 26, Female, 6563412768
Nandhini8, 23, Female, 6785674839
Hashish9, 22, Male, 9453215052
Rahul10, 21, Male, 9998989898

Method III:

SELECT sname, (sid || ', ' || sage || ' ' || sgender||',' || Phonenumber) AS studentdetails from student;

Output:

Student Table

snamestudentdetails
Abhinav1, 22, Male, 9895678909
Ramya2, 24, Female, 6687654634
Preetham3, 21, Male, 9867546453
Nethranand4, 21, Male, 7675643423
Naveen5, 23, Male, 6567784532
Harshita6, 22, Female, 9867546231
Bindu7, 26, Female, 6563412768
Nandhini8, 23, Female, 6785674839
Hashish9, 22, Male, 9453215052
Rahul10, 21, Male, 9998989898

Alias Table Example:

Change Student Table name as Student Data

select * from Student Table AS Student Data;

Output:

      Student Data

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898

Change Student Table as Student Details

select * from Student Table AS Student Details;

Output:

Student Details

sidsnamesagesgenderPhonenumber
1Abhinav22Male9895678909
2Ramya24Female6687654634
3Preetham21Male9867546453
4Nethranand21Male7675643423
5Naveen23Male6567784532
6Harshita22Female9867546231
7Bindu26Female6563412768
8Nandhini23Female6785674839
9Hashish22Male9453215052
10Rahul21Male9998989898