Skip to content

Latest commit

 

History

History
109 lines (89 loc) · 1.82 KB

sql.md

File metadata and controls

109 lines (89 loc) · 1.82 KB

Creation & Insertion

Creating a new table :

CREATE TABLE <table name> (
		<column name> <data type>,
		<column name> <data type>,
		.
		.
		.
		<column name> <data type>
);

Inserting values into a table :

INSERT INTO <table name> VALUES (<val1>,<val2>,....,<valn>);

Inserting values in some columns :

INSERT INTO <table name>(<column name>,<column name>) 
VALUES (<val1>,<val2>);

Data type is important i.e for strings use quotes (ex : ‘python’ instead of python)

Create table from existing table :

CREATE TABLE <table name> AS(
	SELECT <column name>,<column name> 
	FROM <table name>
);

Visualization

To get description of a table :

DESCRIBE <table name>

or (DESC command runs on only the software system// doesn’t run on editors)

DESC <table name>

Selection

To get all values :

SELECT * FROM <table name> ;

To get specific columns :

SELECT <column name>, <column name> FROM <table name> ;

This can also be achieved by :

SELECT ALL <column name> FROM <table name>

To get used to specify the number of records in return:

SELECT <column_name>
FROM <table name>
WHERE condition
LIMIT number;

To get all unique values from a column :

SELECT DISTINCT <column name> FROM <table name> ;

Where Conditionals

To select specific rows with a condition :

SELECT <column name> 
FROM <table name>
WHERE <condition>

Manipulation

Delete all records :

DELETE FROM <table name>

Delete with a condition :

DELETE FROM <table name> WHERE <condition>

Example :

  1. Delete ‘Yash’ student data.
DELETE FROM studentdata WHERE studentname= "Yash";

Update values in a column :

UPDATE <table name>
SET <column name> = <val1>, <column name> = <val2>
WHERE <condition>;