Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 57 additions & 39 deletions database-commands/mysql_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ Create Alias (alternate names) a column
.. code-block:: sql

SELECT Name AS 'country-name', Population as 'country-population'
FROM country;
FROM Country;


Order the rows by Name column in ascending order

.. code-block:: sql

SELECT *
FROM country
FROM Country
ORDER BY Name ASC;


Expand All @@ -85,7 +85,7 @@ Order the rows by Name column in descending order
.. code-block:: sql

SELECT *
FROM country
FROM Country
ORDER BY Name DESC;


Expand All @@ -94,7 +94,7 @@ First order by Continent and then Name within each Continent
.. code-block:: sql

SELECT Name, Continent
FROM country
FROM Country
ORDER BY Continent, Name DESC;


Expand All @@ -103,7 +103,7 @@ Order by content name in descending order and then name in ascending order
.. code-block:: sql

SELECT Name, Continent
FROM country
FROM Country
ORDER BY Continent DESC, Name;


Expand All @@ -119,93 +119,101 @@ Offset

.. code-block:: sql

SELECT * from Country ORDER BY Name LIMIT 5 OFFSET 5;
SELECT *
FROM Country
ORDER BY Name
LIMIT 5 OFFSET 5;

Where clause

.. code-block:: sql

SELECT Name,Code,population from Country WHERE population > 100000000 ORDER BY population;
SELECT Name, Code, Population
FROM Country
WHERE Population > 100000000
ORDER BY Population;

Null clause

.. code-block:: sql

SELECT Name,Code,population
SELECT Name, Code, Population
FROM Country
WHERE population > 100000000 OR population IS NULL
ORDER BY population;
WHERE Population > 100000000 OR Population IS NULL
ORDER BY Population;


OR clause

.. code-block:: sql

SELECT Name,Code,population
SELECT Name, Code, Population
FROM Country
WHERE population > 100000000 OR population IS NULL
ORDER BY population;
WHERE Population > 100000000 OR Population IS NULL
ORDER BY Population;


NOT Null clause

.. code-block:: sql

SELECT Name,Code,population
SELECT Name, Code, Population
FROM Country
WHERE population > 100000000 OR population IS NOT NULL
ORDER BY population;
WHERE Population > 100000000 OR Population IS NOT NULL
ORDER BY Population;


Null clause
AND clause

.. code-block:: sql

SELECT Name,Code,population
SELECT Name, Code, Population
FROM Country
WHERE population > 100000000 AND continent = 'Asia'
ORDER BY population;
WHERE Population > 100000000 AND Continent = 'Asia'
ORDER BY Population;

Where clause with string match

.. code-block:: sql

SELECT Name,Code from Country WHERE Code = 'USA’;
SELECT Name, Code
FROM Country
WHERE Code = 'USA’;


Like clause

.. code-block:: sql

SELECT Name, continent, population
SELECT Name, Continent, Population
FROM Country
WHERE name like 'ind%’;
WHERE Name LIKE 'ind%’;


Like clause with single character

.. code-block:: sql

SELECT Name, continent, population
SELECT Name, Continent, Population
FROM Country
WHERE name like ‘_a%’;
WHERE Name LIKE ‘_a%’;


IN clause

.. code-block:: sql

SELECT Name, continent, population
SELECT Name, Continent, Population
FROM Country
WHERE continent IN ('Asia', 'Europe')
ORDER BY continent;
WHERE Continent IN ('Asia', 'Europe')
ORDER BY Continent;


Regex

.. code-block:: sql

SELECT Name, continent, population
SELECT Name, Continent, Population
FROM Country
WHERE Name REGEXP '^.[a-d].*’;

Expand All @@ -221,7 +229,7 @@ Insert row
.. code-block:: sql

INSERT INTO test
VALUES (1, 'First Value’, ’Second Value')
VALUES (1, 'First Value’, ’Second Value');


Insert row specifying the columns
Expand All @@ -244,7 +252,8 @@ Updating a row

.. code-block:: sql

UPDATE test SET c = ’Something else’
UPDATE test
SET c = ’Something else’
WHERE a = 1;


Expand All @@ -254,7 +263,7 @@ Delete a row with only its first occurrance

DELETE
FROM test
WHERE a=1
WHERE a = 1
LIMIT 1;
SELECT * FROM test;

Expand All @@ -263,7 +272,7 @@ Delete a table

.. code-block:: sql

DROP TABLE test
DROP TABLE test;


Describe table <— MySQL specific
Expand All @@ -284,7 +293,7 @@ Index while creating table

.. code-block:: sql

INDEX(a)
INDEX(a);


Show index
Expand All @@ -298,21 +307,24 @@ Modify the table at a later stage

.. code-block:: sql

ALTER TABLE test ADD d VARCHAR(10);
ALTER TABLE test
ADD d VARCHAR(10);

Remove a column

.. code-block:: sql

ALTER TABLE test DROP d;
ALTER TABLE test
DROP d;

Add a column with more options

.. code-block:: sql

ALTER TABLE test
ADD d VARCHAR(10)
AFTER a DEFAULT ’something’;
AFTER a
DEFAULT ’something’;


Timezone
Expand All @@ -328,7 +340,10 @@ String functions - Length of a value <— counts the bytes

.. code-block:: sql

SELECT Name, LocalName, Length(LocalName) AS len FROM Country where Continent = 'Europe' ORDER BY len;
SELECT Name, LocalName, Length(LocalName) AS len
FROM Country
WHERE Continent = 'Europe'
ORDER BY len;

CHAR_LENGTH counts characters

Expand Down Expand Up @@ -392,7 +407,10 @@ GROUP BY

.. code-block:: sql

SELECT Continent, COUNT(*) as count from Country GROUP BY Continent ORDER BY count DESC;
SELECT Continent, COUNT(*) as Count
FROM Country
GROUP BY Continent
ORDER BY Count DESC;


Scan the table and get the count of distinct values
Expand Down