How Can I Write SQL Queries?

2

2 Answers

Tausif Akram Profile
Tausif Akram answered
Before knowing how to write queries in SQL it is important to know what the different types of SQL queries that you can write are. The following are the four main categories of SQL queries:

• DRL (Data Retrieval Language)
• DML ( Data Manipulation Language)
• DDL ( Data Definition Language)
• DCL (Data Controlling Language)

There names are just given only for categorization so that you may have a better understanding of SQL queries. DRL contain the queries that are used to retrieve or fetch data from the database, such as SELECT statement. You can fetch data from table in accordance to your requirement. DML means the queries that can manipulate the data such as UPDATE, DELETE and INSERT. Likewise DDL means the queries that help in creating and defining the data object such as Tables, Views, Synonyms and Indexes and these queries include CREATE, ALTER. Whereas DCL contain the queries that are used to create new users and for controlling their access to database. Here is an example of SQL queries:

DRL

SELECT FROM EMP
WHERE EMPNO=7788;

DML

UPDATE EMP
SET SAL=2000
WHERE ENAME='KING';

DELET FROM DEPT
WHERE LOC='DALLAS';

DDL

CREATE TABLE EMP
(EMPNO NUMBER (4), ENAME VARCHAR2(10), JOB VARCHAR2(20));

DCL

CREATE USER SCOTT
IDENTIFIED BY TIGER;
John Wright Profile
John Wright answered
SQL is a programing language designed specifically to add, delete, update information in a relational database. The databse you are using will usually offer several methods of creating a query. a common one is called query by example where you simply select from a graphical display the tables and fields that you wish to be returned, and the program writes the SQL for you. You can often access a code version of the query and see the SQL it has created. To edit this, simply type in the changes you wish to make.

But power users of databases often simpy write the SQL by hand and save it to a file. They can use either a command line interface where they type in the SQL, or a simple text editor and then cut and paste the code into the database. The text editor option is very much better, as you can save your first attempt and if it goes wrong simply edit it and try again. The command line way can be frustrating as editing it is trickier. Also when using a sub-query, you can create the query in steps and then cut and paste the working parts together in the editor much more safely than trying to re-type the whole thing by hand at the command line.

Answer Question

Anonymous