Advertisement

Responsive Advertisement

SQL Select Statement With PHP

 


SQL SELECT Statement

The SELECT statement is the most commonly used command in Structured Query Language. It is used to access the records from one or more database tables and views. It also retrieves the selected data that follow the conditions we want.

By using this command, we can also access the particular record from the particular column of the table. The table which stores the record returned by the SELECT statement is called a result-set table.

Syntax of SELECT Statement in SQL

  1. SELECT Column_Name_1, Column_Name_2, ....., Column_Name_N FROM Table_Name;  

In this SELECT syntax, Column_Name_1, Column_Name_2, ….., Column_Name_N are the name of those columns in the table whose data we want to read.

If you want to access all rows from all fields of the table, use the following SQL SELECT syntax with * asterisk sign:

  1. SELECT * FROM table_name;  

Examples of SELECT Statement in SQL

Here, we took the following two different SQL examples which will help you to execute the SELECT statement for retrieving the records:

Example 1:

Firstly, we have to create the new table and then insert some dummy records into it.

Use the following query to create the Student_Records table in SQL:

  1. CREATE TABLE Student_Records   
  2. (  
  3. Student_Id Int PRIMARY KEY,    
  4. First_Name VARCHAR (20),    
  5. Address VARCHAR (20),    
  6. Age Int NOT NULL,  
  7. Percentage Int NOT NULL,  
  8. Grade VARCHAR (10)   
  9. ) ;  

The following query inserts the record of intelligent students into the Student_Records table:

  1. INSERT INTO Student VALUES (201, Akash, Delhi, 18, 89, A2),   
  2. (202, Bhavesh, Kanpur, 19, 93, A1),  
  3. (203, Yash, Delhi, 20, 89, A2),    
  4. (204, Bhavna, Delhi, 19, 78, B1),  
  5. (05, Yatin, Lucknow, 20, 75, B1),  
  6. (206, Ishika, Ghaziabad, 19, 51, C1),  
  7. (207, Vivek, Goa, 20, 62, B2);  

The following SQL query displays all the values of each column from the above Student_records table:

  1. SELECT * FROM Student_Records;  

The output of the above query is:

Student_IDFirst_NameAddressAgePercentageGrade
201AkashDelhi1889A2
202BhaveshKanpur1993A1
203YashDelhi2089A2
204BhavnaDelhi1978B1
205YatinLucknow2075B1
206IshikaGhaziabad1991C1
207VivekGoa2080B2

Post a Comment

0 Comments