Retrieve Data using Query
In order to describe the data structure and to modify the data in the database (Open office and Libre Office), queries are used as instructions. A query enables the joining and filtering of data from various tables.
Database Languages having two type:
- DDL (Data Definition Language)
- DML (Data Manipulation Language)
DDL Statements:
- Create: Using this statement, a database or set of tables can be created.
- Alter: This statement is used to change the table’s structure.
- Drop: This statement is used to remove database objects from the system.
DML statements:
- SELECT: The statement “SELECT” is used to get data from the database.
- INSERT: The statement “INSERT” is used to add a new record to the database.
- DELETE: The database can be cleaned out by using the statement DELETE.
- UPDATE:This statement is used to modify the database’s information.
Database Query
Query is a computer languages. In order to describe the data structure and to modify the data in the database, queries are used as instructions. Query can extract particular data from a database. We can filter and join data from various tables with the help of a query. By using the criteria you supply query will filter the data.
Select Statement
A select query is a language in a database that displays data in Datasheet view. Data from tables is displayed by a query rather than being stored by it. A query may display data from one or more tables, from other queries, or from both of these sources simultaneously.
The SELECT statement has many optional clauses:
- WHERE specifies which rows to retrieve.
- ORDER BY specifies an order in which to return the rows.
Syntax of Select Statement is –
SELECT * FROM <TABLENAME>;
Query related to Simple Select Statement –
Table Name : Product
Product_No | Product_Name | Price | Quantity |
---|---|---|---|
29 | Mouse | 140 | 60 |
51 | Keyboard | 700 | 50 |
15 | Monitor | 3500 | 15 |
42 | CPU | 12000 | 100 |
Question : Write a Query to display all record from the table.
Solution : Select * from product;
Output:
Product_No | Product_Name | Price | Quantity |
---|---|---|---|
29 | Mouse | 140 | 60 |
51 | Keyboard | 700 | 50 |
15 | Monitor | 3500 | 15 |
42 | CPU | 12000 | 100 |
Question : Write a Query to display product name from the table.
Solution : Select Product_Name from product;
Output:
Product_Name |
Soap |
Powder |
Shampoo |
Soap Box |
Question : Write a Query to display Product_Name and Price from the table.
Solution : Select Product_Name, Price from product;
Output:
Product_Name | Price |
---|---|
Soap | 40 |
Powder | 80 |
Shampoo | 300 |
Soap Box | 120 |
Query related to Select Statement with Mathematical function:
Table Name : Product
Product_No | Product_Name | Price | Quantity |
---|---|---|---|
29 | Mouse | 140 | 60 |
51 | Keyboard | 700 | 50 |
15 | Monitor | 3500 | 15 |
42 | CPU | 12000 | 100 |
Question : Write a Query to find the total no of quantity available in table.
Solution : Select sum(quantity) from product;
Output:
sum(quantity) |
---|
225 |
Question : Display the total amount of each item. The amount must be calculated as the price multiplied by quantity for each item.
Solution : Select Product_No, Product_Name, Price * Quantity from product;
Output:
Product_No | Product_Name | Price*Quantity |
---|---|---|
29 | Mouse | 8400 |
51 | Keyboard | 35000 |
15 | Monitor | 52500 |
42 | CPU | 120000 |
Question : Write a query to find the average price of the total product;
Solution : Select avg(price) from product;
Output:
avg(price) |
---|
4085 |
Query related to Select Statement with Where clause.
Table Name : Product
Product_No | Product_Name | Price | Quantity |
---|---|---|---|
29 | Mouse | 140 | 60 |
51 | Keyboard | 700 | 50 |
15 | Monitor | 3500 | 15 |
42 | CPU | 12000 | 100 |
Question : Write a Query to display the product whose price is less than 90.
Solution : Select * from product where price > 750;
Output:
Product_No | Product_Nam | Price | Quantity |
---|---|---|---|
15 | Monitor | 3500 | 15 |
42 | CPU | 12000 | 100 |
Question : Write a Query to find the total amount of the Mouse product;
Solution : Select Price*Quantity from product where Product_Name = ‘Mouse’;
Output:
Price*Quantity |
---|
8400 |
Question : Write a Query to display the data whose quantity is equal to 50.
Solution : Select * from product where quantity = 50;
Output:
Product_No | Product_Nam | Price | Quantity |
---|---|---|---|
51 | Keyboard | 700 | 50 |
Question : Write a Query to display a list of Products whose Price between 150 to 850.
Solution : Select * from product where Price >= 150 and Price <= 850;
Output:
Product_No | Product_Nam | Price | Quantity |
---|---|---|---|
51 | Keyboard | 700 | 50 |
Query related to Select Statement with Order by:
Table Name : Product
Product_No | Product_Name | Price | Quantity |
---|---|---|---|
29 | Mouse | 140 | 60 |
51 | Keyboard | 700 | 50 |
15 | Monitor | 3500 | 15 |
42 | CPU | 12000 | 100 |
Question : Write a Query to display the list of Product_Name in alphabetical order/ascending order.
Solution : Select * from product order by Product_Name ASC;
Or
Solution : Select * from product order by Product_Name;
Output:
Product_No | Product_Nam | Price | Quantity |
---|---|---|---|
42 | CPU | 12000 | 100 |
51 | Keyboard | 700 | 50 |
15 | Monitor | 3500 | 15 |
29 | Mouse | 140 | 60 |
Note : For Descending order use Desc. Example: Select * from product order by Product_Name Desc; This query will show Product_Name in Descending order
Question – Write a Query to display the list of Price in descending order.
Select * from product order by Price DESC;
Output –
Product_No | Product_Nam | Price | Quantity |
45 | Shampoo | 250 | 25 |
52 | Soap Box | 120 | 100 |
31 | Powder | 80 | 30 |
25 | Soap | 40 | 80 |
UPDATE statement
To edit or update already-existing records in a table, use the UPDATE statement. Using the WHERE clause, you can either define a specific subset of entries to edit or use it to update everything at once.
Syntax of Update Statement –
UPDATE <table name> SET = value [, column_name = value ...] [WHERE ];
Table Name – product
Product_No | Product_Name | Price | Quantity |
25 | Soap | 40 | 80 |
31 | Powder | 80 | 30 |
45 | Shampoo | 250 | 25 |
52 | Soap Box | 120 | 100 |
Question – Write a Query to update the price of Shampoo in the product table.
Update product Set Price = 300 where Price = 250;
Output –
Product_No | Product_Name | Price | Quantity |
25 | Soap | 40 | 80 |
31 | Powder | 80 | 30 |
45 | Shampoo | 300 | 25 |
52 | Soap Box | 120 | 100 |
Question – Write a Query to update the Quantity of Powder in the product table.
Update product Set Quantity = 50 where Product_Name = ‘Powder’;
Output –
Product_No | Product_Name | Price | Quantity |
25 | Soap | 40 | 80 |
31 | Powder | 80 | 50 |
45 | Shampoo | 250 | 25 |
52 | Soap Box | 120 | 100 |
Create Table
To create a new table in the database you can use Create Table Command.
Syntax of Create Table –
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype);
Question – Write a Query to create the following table in the database;
Table Name – product
Field | DataType |
Product_No | Integer |
Product_Name | Varchar(20) |
Price | Integer |
Quantity | Integer |
Create table product ( Product_No Int, Product_Name Varchar(20), Price Int, Quantity Int);
Output –
Product_No | Product_Name | Price | Quantity |
Insert Table
Insert statement is primarily used to add a single or more rows to the target table.
Syntax of Insert Table –
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
Or
INSERT INTO table_name VALUES (value1, value2, value3, ...);
Table Name – product
Product_No | Product_Name | Price | Quantity |
25 | Soap | 40 | 80 |
31 | Powder | 80 | 30 |
45 | Shampoo | 250 | 25 |
52 | Soap Box | 120 | 100 |
Question – Write a Query to add a new row with the following details
(72, “Hair Conditioner‟, 350, 60)
Insert into product (Product_No, Product_Name, Price, Quantity) values(72, “Hair Conditioner‟, 350, 60);
Or
Insert into product values(72, “Hair Conditioner‟, 350, 60);
Output –
Product_No | Product_Name | Price | Quantity |
25 | Soap | 40 | 80 |
31 | Powder | 80 | 30 |
45 | Shampoo | 300 | 25 |
52 | Soap Box | 120 | 100 |
72 | Hair Conditioner | 350 | 60 |