How To Write Sql Queries

Table of contents:

How To Write Sql Queries
How To Write Sql Queries

Video: How To Write Sql Queries

Video: How To Write Sql Queries
Video: Best Way to Write Basic SQL Queries 2024, December
Anonim

SQL is a query language that is commonly used to perform operations within a specific DBMS. Having mastered SQL, you can write various web applications using a MySQL or Oracle database. Using the query language, all tables in the database are created, and certain data are stored, changed and retrieved.

How to write sql queries
How to write sql queries

Instructions

Step 1

SQL commands can be divided into several groups:

- DDL, which are used to change and delete objects within the database;

- DCL, which control access to the database;

- TCL, which determine the outcome of transactions;

- DML, which are responsible for moving data.

Step 2

The most basic command for creating queries is SQL Create Table. It describes the structure of the generated table. As part of this query, you can define local columns that define the type and names of the data in a given column. For example:

CREATE TABLE Firsttable (id int, name varchar (255), surname varchar (255));

The query will create Firsttable with slots id, name, and surname, which can be set to appropriate values.

Step 3

Another important command is INSERT, which inserts specific data into an already created table, and has the syntax:

INSERT INTO `table` (` column1`, `column2`) VALUES (“val1”,“val2”)

Where column1, column2 are the created columns, and val1 and val2 are the values you want to insert.

Step 4

To retrieve data for output or other operations, a SELECT query is used, which looks like:

SELECT * FROM `table`

Additionally, you can set parameters for extracting data from any column separately. For example, if we want to retrieve name from Firsttable, then the query would look like this:

SELECT * FROM `Firsttable` WHERE name =‘ $ name ’

Step 5

You can write a query in a.txt or.sql file using regular Notepad. Write your commands, after which you can load them, for example, through the phpMyAdmin interface in the control panel of your hosting or DBMS.

Recommended: