How To Write A SQL Query

Table of contents:

How To Write A SQL Query
How To Write A SQL Query

Video: How To Write A SQL Query

Video: How To Write A SQL Query
Video: Best Way to Write Basic SQL Queries 2024, November
Anonim

Querying relational databases using Structured Universal Computing Language SQL is the recognized standard for managing data in a DBMS. Due to its versatility, the SQL language has become widespread on the web resources of the World Wide Web. Writing SQL queries is based on the application of several basic rules for working with a relational database. Writing a SQL query will help you implement the tasks of selecting specific information from tables, adding, modifying or deleting rows in a table.

How to write a SQL query
How to write a SQL query

Instructions

Step 1

To get the stored information from the database tables, create a select query - SELECT. If there are links between tables, data can be taken according to appropriate conditions from any columns of related tables. List all required columns after the SELECT statement. Specify the tables used in the query in the FROM clause. In its simplest form, a select query displays all rows of the specified columns in a given table: SELECT col1, col2 FROM my_table.

Step 2

If necessary, set a condition for selecting rows. The condition is set by the WHERE clause. Set the value of the parameter you want after this instruction. Function calculation and comparison operations can also be used here. For example, a statement of the form WHERE col1> 3 allows you to display table rows in which the value of the col1 column is greater than 3. To set the required expression, use combinations of AND, OR operators, as well as conditional operators of the SQL language.

How to write a SQL query
How to write a SQL query

Step 3

To insert new rows into a table, write an INSERT query. With its help, you can insert new data of the same type as already existing in the table. The syntax for this statement is very simple: INSERT INTO my_table (col1, col2, col3) VALUES (‘new_data1’, ‘new_data2’, ‘new_data3’). Here, the VALUES statement sets new row values to each existing column in my_table.

Step 4

Changes to data in any row of the table are performed using the UPDATE query. Moreover, it is possible to set a WHERE selection condition, in which the information in the database is changed. Define the data to change and the condition for your request. To do this, write a line like this: UPDATE my_table SET col1 = ‘new_data1’, col3 = ‘new_data3’ WHERE col3 = 10. The query will perform the data change specified in the SET statement only if the condition in the WHERE clause is satisfied.

Step 5

A DELETE statement is written to delete an entire row from a data table. Moreover, the row is deleted only when the WHERE condition is set. Write the expression: DELETE FROM my_table WHERE col1 = ‘data1’. Executing this query will delete the table row containing the value data1 in the col1 column.

Recommended: