SQL Commands: Difference between revisions

From Fact Weaver
No edit summary
No edit summary
Line 47: Line 47:
SELECT COUNT(*)
SELECT COUNT(*)
FROM people;
FROM people;
</nowiki>
 
<nowiki>
Placing a column in the count command will count all the non missing values.
Placing a column in the count command will count all the non missing values.
This will count the number of birth dates present in the people table
This will count the number of birth dates present in the people table
SELECT COUNT(birthdate)
SELECT COUNT(birthdate)
FROM people;
FROM people;
</nowiki>
 
<nowiki>
To count the number of distinct values, the following can be used
To count the number of distinct values, the following can be used
SELECT COUNT(DISTINCT birthdate)
SELECT COUNT(DISTINCT birthdate)
FROM people;
FROM people;
</nowiki>
</nowiki>

Revision as of 02:40, 12 September 2023

Below I have listed the most common commands that I am using when acquiring data from a database

Selecting a single Column from a table:

This will select the column "Name" from the "People" Table.
SELECT name 
FROM people; 

Selecting multiple columns from a table:

This will select the Name and Birthrate column from the people table.
SELECT name, birthdate
FROM people; 

This will select all columns from a table.
SELECT *
FROM people; 

This will select 10 rows with all columns from a table. 
SELECT *
FROM people
LIMIT 10; 

This will select 10 rows with all columns from a table.
SELECT *
FROM people
LIMIT 10; 

Selecting all unique values from a column:

This will select all the unique values in the language column from the films Table.
SELECT DISTINCT language
FROM films;

Counting in a table:

This will count all the rows in the people table.
SELECT COUNT(*)
FROM people;

Placing a column in the count command will count all the non missing values.
This will count the number of birth dates present in the people table
SELECT COUNT(birthdate)
FROM people;

To count the number of distinct values, the following can be used
SELECT COUNT(DISTINCT birthdate)
FROM people;