0% found this document useful (0 votes)
1 views4 pages

SQL Commands Syntax

The document outlines the syntax and examples for various SQL commands, including creating databases and tables, inserting records, modifying data, and querying information. It also covers aggregate functions, the HAVING clause, and the use of relational and logical operators. Additionally, it provides examples of how to filter and sort data using different SQL techniques.

Uploaded by

yuvaram9790
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

SQL Commands Syntax

The document outlines the syntax and examples for various SQL commands, including creating databases and tables, inserting records, modifying data, and querying information. It also covers aggregate functions, the HAVING clause, and the use of relational and logical operators. Additionally, it provides examples of how to filter and sort data using different SQL techniques.

Uploaded by

yuvaram9790
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Commands Syntax

Syntax Examples
Creating the CREATE DATABASE <DATABASENAME>; CREATE DATABASE SCHOOL;
database
Open the Use <DATABASENAME> ; Use SCHOOL;
database
Creating the CREATE TABLE <TABLENAME> CREATE TABLE
table (<COLMNNAME1><DATATYPE1><SIZE>, STUDENT(STUID INT(3),NAME
<COLUMNNAME2><DATATYPE2><SIZE> ; CHAR(30));
Insert the records INSERT INTO <TABLENAME> INSERT INTO STUDENT
into the table VALUES(<VALUE1,VALUE2…>); VALUES(1,’JOHN’);
INSERT INTO STUDENT
VALUE(2,’NILA’);
Structure of the DESC <TABLE NAME> DESCRIBE STUDENT
table OR OR
DESCRIBE STUDENT DESC STUDENT
Display all the SHOW DATABASES;
databases
Display the tables SHOW TABLES ;
in the particular
database
Delete the DROP <DATABASNAME>; DROP STUDENT;
database
Delete the table DROP TABLE <TABLEAME>; DROP TABLE STUDENT;
Adding the ALTER TABLE <TABLENAME> ADD ALTER TABLE STUDENT ADD
column in <COLUMNNAME> <DATATYPE>(SIZE); GENDER CHAR(2);
existing table
Adding the olumn ALTER TABLE <TABLENAME> ADD ALTER TABLE STUDENT ADD
with common (<COLUMNAME><DATATYPE> (MARKS INT(2) DEFAULT
data to the CONSTRAINT); 100);
existing table
Rename TABLE ALTER TABLE <current_table_name> ALTER TABLE STUDENT
name RENAME TO <new_table_name>; RENAME TO STU;
Rename the ALTER TABLE <TABLENAME> ALTER TABLE STUDENT
column name CHANGE COLUMN <old_column_name> CHANGE COLUMN CITY STATE
<new_column_name> VARCHAR(15);
<DATATYPE>(SIZE);
Modify the data ALTER TABLE <TABLENAME> MODIFY ALTER TABLE STUDENT
type for the COLUMN(<COLUMNAME><DATATYPE>SIZE) MODIFY COLUMN CITY
particular column ; CHAR(40);
Delete a column ALTER TABLE <TABLENAME> DROP ALTER TABLE STUDENT DROP
from the table (<COLUMNAME> CITY;
Updating an UPDATE <TABLENAME> UPDATE STUDENT SET
existing record SET column1 = value1, column2 = CITY = CHENNAI WHERE
value2, ...[WHERE condition]; STUID=2;
UPDATE products SET
price = price * 1.1;
#Increase all prices by 10%
Display the SELECT * FROM <TABLENAME>; SELECT * FROM STUDENT;
whole records
Display the SELECT * FROM <TABLENAME> WHERE SELECT * FROM STUDENT
records CONIDITION; WHERE ROLLNO=101;
according to the
condition
Display single SELECT <COLUMNNAME> FROM SELECT CITY FROM
column record <TABLENAME>; STUDENT;
Display the SELECT <COLUMNNAME> FROM SELECT NAME FROM STUDENT
record in single <TABLENAME> WHERE CONDITION; WHERE ROLLNO=101;
column
according to the
condition
Eliminate SELECT DISTINCT <COLUMN NAME> SELECT DISTINCT(age) FROM
duplicate values FROM <TABLENAME>; STUDENT;
USING RELATIONAL OPERATORS(>,<,<=,>=,==,!=/<>/!=)
Applying the SELECT <COLUMNNAME> FROM SELECT MARKS FROM STUDENT
relational <TABLENAME> WHERE WHERE ROLLNO>100;
operators using <COLUMNNAME><CONDITION> SELECT NAME,MARKS FROM
where clause STUDENT WHERE ROLLNO>=100;
USING LOGICAL OPERATORS(AND,OR,NOT)

Applying more SELECT <COLUMNNAME> FROM SELECT NAME FROM STUDENT


than one <TABLENAME> WHERE WHERE MARKS>=80 AND
condition using <COLUMNNAME><CONDITION> AND GENDER=’M’;
<CONDITION>
where clause SELECT <COLUMNNAME> FROM SELECT NAME FROM STUDENT
<TABLENAME> WHERE WHERE MARKS>=80 OR
<COLUMNNAME><CONDITION> OR GENDER=’M’;
<CONDITION>
SELECT <COLUMNNAME> FROM SELECT NAME FROM STUDENT
<TABLENAME> WHERE WHERE MARKS>=80 NOT
<COLUMNNAME><CONDITION> NOT GENDER=’M’;
<CONDITION>
USING ALIAS NAME
Applying the SELECT <COLUMNNAME> FROM SELECT NAME FROM STUDENT
duplicate table <TABLENAME> AS <ALIASNAME>; AS STUDENT1;
name
Applying the SELECT <COLUMNNAME> AS SELECT NAME AS STUDENTNAME
duplicate column <ALIASNAME>FROM <TABLENAME>; FROM STUDENT;
name
Including the text SELECT <COLUMNNAME> SELECT ROLLNO,NAME ,‘WAS
message between ‘TEXTMESSAGE’ <COLUMNNAME> FROM BORN ON’,DOB FROM STUDENT;
the columns <TABLENAME>;
SPECIAL OPERATORS ..BETWEEN ..AND
Apply the SELECT <COLUMNNAME> FROM SELECT ROLLNO,MARKS FROM
‘between and not <TABLENAME> WHERE <COLUMNNAME> STUDENT WHERE MARKS
between’ BETWEEN <VALUE1> AND <VALUE2>; BETWEEN 40 AND 80;
SELECT <COLUMNNAME> FROM SELECT ROLLNO,MARKS FROM
<TABLENAME> WHERE <COLUMNNAME> NOT STUDENT WHERE MARKS NOT
BETWEEN <VALUE1> AND BETWEEN 40 AND 80;
<VALUE2>;
CONDITION BASED ON A LIST – IN AND NOT IN
Apply the ‘ in and SELECT <COLUMNNAME> FROM SELECT ROLLNO FROM STUDENT
not in’ values <TABLENAME> WHERE <COLUMNNAME> IN WHERE CITY IN MUMBAI;
<VALUE1>,<VALUE2>…
SELECT <COLUMNNAME> FROM SELECT ROLLNO FROM STUDENT
<TABLENAME> WHERE <COLUMNNAME> NOT WHERE CITY NOT IN MUMBAI;
IN <VALUE1>,<VALUE2>…
CONDTION BASED ON PATTERN—LIKE(USE WILD CARD CHARACTERS %-MATCHES ANY
STRING UNDERSCORE(_) MATCHES ANYONE CHARACTER
-> The LIKE operator is SELECT <COLUMNAME> SELECT * FROM STUDENT WHERE NAME
used in a WHERE clause FROM <TABLENAME> WHERE LIKE “D%”;
to search for a specified <COLUMNNAME> LIKE SELECT * FROM STUDENT WHERE
pattern in a column. <PATTERN>; NAME LIKE “%D%”;
SELECT * FROM STUDENT WHERE NAME
-> The percent sign (%) LIKE “_D%”;
represents zero, one, or SELECT * FROM STUDENT WHERE
multiple characters NAME LIKE “%D”;
-> The underscore sign (_)
represents one, single
character
order by select * from CUSTOMER order by
Address, LastName
select * from CUSTOMER order by
LastName desc
SELECT department, COUNT(*) AS
HAVING with COUNT() employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;
SELECT product_id, SUM(quantity
HAVING with SUM() * price) AS total_sales
FROM order_items
GROUP BY product_id
HAVING SUM(quantity * price) >
10000;
SELECT category, AVG(price) AS
HAVING with AVG() avg_price
FROM products
GROUP BY category
HAVING AVG(price) > 50;
COUNT() SELECT department, COUNT(*) AS
employee_count
Aggregate Functions in FROM employees
GROUP BY GROUP BY department;
SUM() SELECT product_id, SUM(quantity
* price) AS total_sales
FROM order_items
GROUP BY product_id;
AVG() SELECT job_title, AVG(salary)
AS avg_salary
FROM employees
GROUP BY job_title;
MIN() SELECT customer_id,
MAX() MIN(order_date) AS
first_order,
MAX(order_date) AS
latest_order
FROM orders
GROUP BY customer_id;
SELECT column_name(s), SELECT department, COUNT(*) AS
aggregate_function(col employee_count
GROUP BY umn_name) FROM employees
FROM table_name GROUP BY department;
WHERE condition
GROUP BY SELECT
customer_id,
column_name(s)
COUNT(*) AS order_count,
[ORDER BY
column_name(s)]; SUM(amount) AS total_spent
FROM orders
GROUP BY customer_id
HAVING SUM(amount) > 1000;

The HAVING clause in MySQL filters groups of rows after the GROUP BY clause has been
applied. Unlike WHERE which filters rows before grouping, HAVING filters the grouped
results.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy