Consider the tables provided below and answer the following SQL questions
Question:
Consider the tables provided below and answer the following SQL questions:
Table 1: Person.
P_Id | LastName | FirstName | Address | City |
1 | Jason | Rita | Bella Boulevard | Sydney |
2 | Smith | Jackson | High Park | Sydney |
3 | Wagner | James | Richmond | Perth |
Table 2: Order.
O_Id | OrderNo | P_Id |
1 | 77895 | 3 |
2 | 44678 | 3 |
3 | 22456 | 1 |
4 | 24562 | 1 |
5 | 34764 | 2 |
Answer the following SQL questions:
- Write insert statements to enter the first two records into Table 1 as shown in the above figure.
- Display a list that presents the data contained in Table 1 sorted by column 2 in descending order.
- Display the first name, last name, and city for the person who has placed the order with OrderNo ‘77895’.
- How many persons belong to Sydney city.
Summary:
In this question, they have given two tables. And we have asked to do certain operations by using SQL commands to get the output.
Explanation:
SQL (Structured Query Language) is a hybrid language used for handling relational database management systems.
SQL is categorized into four types they are:
1)DDL-Data Definition Language.
Commands of data definition language are:
- Create
- Alter
- Rename
- Drop
- Truncate
2) DML-Data Manipulation Language.
Commands for data manipulation language:
- Insert
- Select
- Update
- Delete
3) DQL-Data Query Language.
Data query language Command:
- Select
4)DCL-Data Control Language.
Commands of data control language:
- Grant
- Revoke
By using these commands we can answer the above questions.
- From using create command (CREATE Table Tablename) we can create the table person and order_ respectively.
- And by using the insert command (INSERT into table name values(value1,value2,…..)) insert the values for table person and order_.
- Next, use the Select command(SELECT * from table name) for performing some actions from the given tables.
Source code:
Code for doing SQL commands.
CREATE TABLE Person ( P_Id int primary key, Lastname char[8], Firstname char[8], Address varchar[15], City char[7] ); CREATE TABLE Order_ ( O_Id int primary key, OrderNo int, P_Id int, foreign key(P_Id) references Person ); INSERT INTO Person VALUES (1,'Jason','Rita','Bella Boulevard','Sydney'); INSERT INTO Person VALUES (2,'Smith','Jackson','High Park','Sydney'); INSERT INTO Person VALUES (3,'Wagner','James','Richmond','Perth'); INSERT INTO Order_ VALUES (1,77895,3); INSERT INTO Order_ VALUES (2,44678,3); INSERT INTO Order_ VALUES (3,22456,1); INSERT INTO Order_ VALUES (4,24562,1); INSERT INTO Order_ VALUES (5,34764,2); SELECT * FROM Person order by Lastname desc; SELECT Firstname, Lastname, City from Person, Order_ where Person.P_Id=Order_.P_Id AND OrderNo=77895; SELECT Count(City) from Person where City='Sydney';
Output:
a)
b)
c)
d)