A database is a organized collection of data. Data is stored in relational database in form of tables. To create, retrieve, update and delete from relational database, we use SQL queries. In this guide, we will learn the SQL Syntax.
SQL Statements
SQL statement tells the database that what information you would like to retrieve or what operation you want to perform on the data.
For example:
Consider this table: STUDENT
STUDENT_NAME STU_AGE STU_ADDRESS ------------ ------- ------------ Ajeet 30 Chennai Chaitanya 31 Noida Steve 29 Agra Rahul 30 Gurgaon
SQL statement to fetch STUDENT_NAME from the table STUDENT:
SELECT STUDENT_NAME FROM STUDENT;
To fetch the complete table:
SELECT * FROM STUDENT;
SQL is NOT case sensitive
SQL is not a case sensitive language. For example: Both the following statements would work fine and produce the same output:
select * from student;
&
SELECT * FROM STUDENT;
Semicolon
As we have seen above, we have to end the SQL statement with a semi colon, it tells the RDBMS that this is a complete SQL statement. We can write more than one SQL statements together but we have to end each one of them with semicolon so that the database management system knows that they are different SQL statements. This way RDBMS can serve more than one SQL queries in a single database call.
Syntax of Most Important SQL Commands
Important Note: The following commands are covered separately on our website so you don’t have to get into the details now, just go through them once and practice them after you have gone through the individual tutorial of each of them.
SQL SELECT Statement
To fetch the data from table
SELECT column_name1, column_name2....column_nameN FROM table_name;
SQL WHERE Clause
TO fetch the specific rows from the table that meets the given condition.
SELECT column_name1, column_name2....column_nameN FROM table_name WHERE CONDITION;
SQL ORDER BY Clause
To fetch the records in a particular order
SELECT column_name1, column_name2....column_nameN FROM table_name ORDER BY column_name1, column_name2, ... {ASC|DESC};
SQL GROUP BY Clause
SELECT COUNT(column_name) FROM table_name WHERE CONDITION GROUP BY column_name;
SQL DISTINCT Clause
To fetch distinct values of column from the table.
SELECT DISTINCT column_name1, column_name2....column_nameN FROM table_name;
SQL HAVING Clause
SELECT SUM(column_name) FROM table_name WHERE CONDITION GROUP BY column_name HAVING (arithmetic function condition);
SQL CREATE TABLE Statement
To create the table in the database.
CREATE TABLE table_name( column_name1 data_type, column_name2 data_type, ..... ..... column_nameN data_type, PRIMARY KEY(column(s)) );
SQL DESC Statement
DESC table_name;
SQL INSERT INTO Statement
To insert a record into the table.
INSERT INTO table_name( column_name1, column_name2....column_nameN) VALUES (value_1, value_2.....value_N);
SQL UPDATE Statement
To update data in table.
UPDATE table_name SET column_name1 = value_1, column_name2 = value_2....column_nameN=value_N WHERE CONDITION;
SQL DELETE Statement
To delete rows from table.
DELETE FROM table_name WHERE CONDITION;
SQL DROP TABLE Statement
To delete the table completely from database.
DROP TABLE table_name;
SQL TRUNCATE TABLE Statement
To delete all the records from the table.
TRUNCATE TABLE table_name;
ADD, DROP or MODIFY column of table
ALTER TABLE table_name {ADD|DROP|MODIFY} column_name {data_type};
SQL CREATE INDEX Statement
CREATE UNIQUE INDEX index_name ON table_name ( column_name1, column_name2,...column_nameN);
Dropping Index of table
ALTER TABLE table_name DROP INDEX index_name;
Rename Table name
ALTER TABLE table_name RENAME TO new_table_name;
SQL AND/OR Clause
SELECT column_name1, column_name2....column_nameN FROM table_name WHERE CONDITION_1 {AND|OR} CONDITION_2;
SQL COUNT Clause
SELECT COUNT(column_name) FROM table_name WHERE CONDITION;
SQL IN Clause
Selects rows based on the column values in a given set of values.
SELECT column_name1, column_name2....column_nameN FROM table_name WHERE column_name IN (value_1, value_2,...value_N);
SQL BETWEEN Clause
Selects rows based on the column values in a given range.
SELECT column_name1, column_name2....column_nameN FROM table_name WHERE column_name BETWEEN value_1 AND value_2;
SQL USE Statement
To select a particular database.
USE database_name;
SQL CREATE DATABASE Statement
To create database.
CREATE DATABASE database_name;
SQL DROP DATABASE Statement
Delete the entire database.
DROP DATABASE database_name;
SQL COMMIT Statement
To commit the changes permanently to the database.
COMMIT;
SQL ROLLBACK Statement
To rollback the changes made to the database to a previous checkpoint.
ROLLBACK;
Leave a Reply