SQL Select Query

Write SQL select queries.

The SQL SELECT statement returns a result set of records, from one or more tables.

A SELECT statement retrieves zero or more rows from one or more database tables or database view. In most applications, SELECT is the most commonly used data manipulation language (DML) command.

Example


Table: Customer
+----+-------+
| Id | Name  |    
+----+-------+
1  | Ram   |
2  | Henry |
3  | Sam   |
4  | Shyam |
+----+-------+

Table: Order                             
+----+-------------+----------+--------+--------------+      
| Id | CustomerID  | Product  | Price  | OrderDate    |      
+----+-------------+----------+--------+--------------+      
1  | 1           | Chair    | 500    | 01/02/2018   |      
2  | 2           | Press    | 8000   | 01/04/2018   |      
3  | 3           | Table    | 6000   | 01/04/2018   |      
4  | 2           | Table    | 6000   | 01/06/2018   |
+----+-------------+----------+--------+--------------+      


Query: Select all columns from a single table

// using * sign
SELECT * FROM Customer;

// using fields name
SELECT ID, Name FROM Customer;

Result:

+----+-------+
| Id | Name  |    
+----+-------+
1  | Ram   |
2  | Henry |
3  | Sam   |
4  | Shyam |
+----+-------+

Query: Select particular columns from a single table


SELECT Name FROM Customer;

Result:

+-------+
Name  |    
+-------+
| Ram   |
| Henry |
| Sam   |
| Shyam |
+-------+

Query: Select all columns from multiple tables


SELECT CS.ID CUSTOMERID, CS.NAME CUSTOMERNAME,  OD.ID ORDERID, OD.PRODUCT, OD.PRICE, OD.ORDERDATE 
FROM CUSTOMER CS JOIN ORDERS OD ON(CS.ID=OD.CUSTOMERID);

Result:

|CUSTOMERID |CUSTOMERNAME |ORDERID |PRODUCT |PRICE |ORDERDATE |
|1          |Ram          |1       |Chair   |500   |01-FEB-18 |
|2          |Henry        |4       |Table   |6000  |01-JUN-18 |
|2          |Henry        |2       |Press   |8000  |01-APR-18 |
|3          |Sam          |3       |Press   |8000  |01-APR-18 |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


No comments:

Post a Comment