Different Query Combinations
Case 1: If we want to retrieve attributes ROLL_NO and NAMEof all students, the query will be:
SELECT ROLL_NO, NAME FROM STUDENT;
ROLL_NO | NAME |
1 | RAM |
2 | RAMESH |
3 | SUJIT |
4 | SURESH |
Case 2: If we want to retrieve ROLL_NO and NAME of the students whose ROLL_NO is greater than 2, the query will be:
SELECT ROLL_NO, NAME FROM STUDENT
WHERE ROLL_NO>2;
ROLL_NO | NAME |
3 | SUJIT |
4 | SURESH |
CASE 3: If we want to retrieve all attributes of students, we can write * in place of writing all attributes as:
SELECT * FROM STUDENT
WHERE ROLL_NO>2;
ROLL_NO | NAME | ADDRESS | PHONE | AGE |
3 | SUJIT | ROHTAK | 9156253131 | 20 |
4 | SURESH | DELHI | 9156768971 | 18 |
CASE 4: If we want to represent the relation in ascending order by AGE, we can use ORDER BY clause as:
SELECT * FROM STUDENT ORDER BY AGE;
ROLL_NO | NAME | ADDRESS | PHONE | AGE |
1 | RAM | DELHI | 9455123451 | 18 |
2 | RAMESH | GURGAON | 9652431543 | 18 |
4 | SURESH | DELHI | 9156768971 | 18 |
3 | SUJIT | ROHTAK | 9156253131 | 20 |
Note:
ORDER BY AGEis equivalent to ORDER BY AGE ASC.
If we want to retrieve the results in descending order of AGE, we can use ORDER BY AGE DESC.
CASE 5: If we want to retrieve distinct values of an attribute or group of attribute, DISTINCT is used as in:
SELECT DISTINCT ADDRESS FROM STUDENT;
ADDRESS |
DELHI |
GURGAON |
ROHTAK |
Structured Query Language (SQL)
Structured Query Language is a standard Database language that is used to create, maintain, and retrieve the relational database. In this article, we will discuss this in detail about SQL. Following are some interesting facts about SQL. Let’s focus on that.
SQL is case insensitive. But it is a recommended practice to use keywords (like SELECT, UPDATE, CREATE, etc.) in capital letters and use user-defined things (like table name, column name, etc.) in small letters.
We can write comments in SQL using “–” (double hyphen) at the beginning of any line. SQL is the programming language for relational databases (explained below) like MySQL, Oracle, Sybase, SQL Server, Postgre, etc. Other non-relational databases (also called NoSQL) databases like MongoDB, DynamoDB, etc. do not use SQL.
Although there is an ISO standard for SQL, most of the implementations slightly vary in syntax. So we may encounter queries that work in SQL Server but do not work in MySQL.