One of the most frequently used SQL commands is SELECT. Here are some examples of its usage.
Basic SELECT-FROM-WHERE query
1. Display the birthday and address of the student named ‘Ahmad’.
-> SELECT Bdate, Address
The query “selects” the Student tuples that satisfy the condition of the WHERE clause then takes the result from the Bdate and Address attributes listed in the SELECT clause.
All the queries are of the form SELECT-FROM-WHERE.
2. Retrieve the name and St_Id of all the students who are in ‘Civil’ Branch.
-> SELECT SName , St_Id
FROM Student , Branch
WHERE BName = ‘Civil’ AND Br_No=Bno;
In the query in WHERE clause, BName = ‘Civil’ is the selection condition and ‘Br_No=BNo’ is the join condition . It is used when two tables (here , Student and Branch) need to be joined.
Use of Asterisk “*”
3. Retrieve all attributes of all students with Branch No. = 3.
-> SELECT *
WHERE Br_No = 3;
To retrieve all the attributes of the selected tuples of the table using the WHERE clause, we use ( * ).
4. List the courses in ‘Chemical’ Branch.
-> SELECT CName
FROM Branch as B, Course as C
WHERE BName=’Chemical’ AND B.BNo = C.BNo;
If same name is used for two attributes in different relations, prefixing is done to avoid ambiguity. Aliasing is done. Here Branch is aliased as B and Course is aliased as C. This way, we know which attribute belongs to which table.
SELECT ALL Operator
5. Retrieve the Branch Number of all the courses.
-> SELECT ALL BNo
SELECT ALL displays all the entries of the specified field including the duplicate values. The same value will appear as many times as it has appeared.
Computer Science >