Stuctured Query Language
last edited Thu, 24 Apr 2025 05:59:09 GMT
backlinks: null
Knowing SQL is essential in various computing domains, everyone uses it, even indirectly in many cases.
The Basics direct link to this section
- data in a SQL database is stored based on a predefined schema
- requests are known as queries
- postgres and mySQL are commonly used relational databases used in the industry
- a primary key is used as an unique identifier
- the foreign key is a unique identifier found in another table
- SQL is case insensitive
- for readability always include keywords in all caps, see the example below
SELECT * FROM my_table;
The snippet above queries all the entries in my_table
. If you want to display a specific column, you can just query the columns by name.
SELECT student_name FROM Students;
SELECT student_id, student_name FROM Students;
Keywords direct link to this section
Clauses must always be listed in the order below:
- SELECT
- columns to display
- FROM
- table(s) to pull from
- WHERE
- filter rows
- GROUP BY
- split rows into groups
- HAVING
- filter grouped rows
- ORDER BY
- columns to sort
An example using the WHERE keyword
SELECT * FROM Grades
WHERE student_id = 101
Boolean logic can also be applied to a filter
SELECT student_id, course_name, final_grade FROM Grades
WHERE course_name = 'Intro to ML' AND final_grade > 90;
An example where data is sorted in descending order
SELECT * FROM Grades
ORDER BY final_grade DESC;