You are on page 1of 3

SQL COMMANDS

To set default value for existing columns :

ALTER TABLE tablename


ADD DEFAULT('defvalue') FOR column_name

To display min:
SELECT MIN(column_name)
FROM table_name
WHERE condition;

To display max:
SELECT MAX(column_name)
FROM table_name
WHERE condition;

list the different (distinct) values.

SELECT DISTINCT column1,  column2, ...


FROM table_name;

Order by syntax:
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Insert value in table:


INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2,  value3, ...);

Update statement:
UPDATE table_name
SET column1 = value1,  column2 = value2, ...
WHERE condition;

Delete statement:
DELETE FROM table_name  WHERE condition;

The COUNT() function returns the number of rows that matches a specified


criterion.
SELECT COUNT(column_name)
FROM table_name
WHERE condition;

The AVG() function returns the average value of a numeric column


SELECT AVG(column_name)
FROM table_name
WHERE condition;
The SUM() function returns the total sum of a numeric column. 
SELECT SUM(column_name)
FROM table_name
WHERE condition;

Like operator:

WHERE CustomerName LIKE 'a%' Finds any values that start with "a"

WHERE CustomerName LIKE '%a' Finds any values that end with "a"

WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position

WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second position

WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least 2 ch

WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least 3 ch

WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with "o"

To delete databse:
DROP DATABASE databasename;

Create table:
CREATE TABLE table_name (
     column1 datatype,
     column2 datatype,
     column3 datatype,
   ....
);

To delete table:
DROP TABLE table_name;
To altrer table”:
ALTER TABLE table_name
ADD column_name datatype;

Foreign key:
CREATE TABLE Orders (
    OrderID int NOT NULL,
    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

To delte column:
1. ALTER TABLE "table_name" DROP "column_name";

The GROUP BY statement groups rows that have the same values into summary
rows, like "find the number of customers in each country".

The GROUP BY statement is often used with aggregate functions


(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more
columns.

SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

The UNION operator is used to combine the result-set of two or


more SELECT statements.

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;

You might also like