DEV Community

loading...
Cover image for SQL Cheat Sheet: Top 10 Most Important SQL Commands To Know

SQL Cheat Sheet: Top 10 Most Important SQL Commands To Know

Jack McClelland
Boston College CS student from the Bay Area. Love dev tools, cybersecurity, and undergraduate entrepreneurship. Huge fan of all things outdoors, music, and f1!
・3 min read

As companies and organizations find themselves dealing with rapidly increasing amounts of data, there's a growing need for developers to effectively use databases to handle this data. SQL, which stands for Structured Query Language, is a programming language that helps manage data stored in relational databases (a popular type of database).

SQL commands can help a developer create tables, add and modify data in these tables, search the database, and more. This article will cover a list of ten basic SQL commands that are essential to know for developers working with SQL. You'll find for each SQL command a code snipped and brief description of what the code runs.

Whether you're a beginner or a pro at SQL, consider trying out Arctype, a redesigned SQL client for PostgreSQL and MySQL developers and teams. It's free, fast, and easy-to-use: http://arctype.com/

Alt Text

Let's get right into it!


CREATE TABLE

CREATE TABLE table_name (
  column_1 datatype_1, 
  column_2 datatype_2, 
  column_3 datatype_3
);
Enter fullscreen mode Exit fullscreen mode

This command allows you to create a new database or table; the example above adds a new table with a title and column names.

ALTER TABLE

ALTER TABLE table_name 
ADD column_name datatype;
Enter fullscreen mode Exit fullscreen mode

Run this command to modify (add, drop, rename, etc) the structure (not the data) in your database; the example above adds a new column to a table with a specified datatype.

DELETE

DELETE FROM table_name
WHERE some_condition = some_value;
Enter fullscreen mode Exit fullscreen mode

This command can delete data from your table based on conditions specified with the WHERE keyword.

DROP

DROP TABLE table_name;
Enter fullscreen mode Exit fullscreen mode

Similar to the create command, DROP deletes a database or table. Be careful when using this command – the code above will delete your whole table, including all data, indexes, and more.

ALTER TABLE table_name DROP COLUMN column_name;
Enter fullscreen mode Exit fullscreen mode

The ALTER TABLE and DROP statement above will remove a specific column from a table.

INSERT INTO

INSERT INTO table_name (column_1, column_2, column_3) 
VALUES (value_1, value_2, value_3);
Enter fullscreen mode Exit fullscreen mode

To add new records to your table, use the INSERT INTO command. You can use this command on one or more rows.

SELECT

SELECT column_name 
FROM table_name;
Enter fullscreen mode Exit fullscreen mode

Every query begins with SELECT; this is how you grab data from your database. It's the most fundamental SQL query. After the SELECT command, you can use the keyword FROM to specify a table, the keyword WHERE to select with conditions, and the keyword ORDER BY to sort your results.

UPDATE

UPDATE table_name
SET some_column = some_value
WHERE some_column = some_value;
Enter fullscreen mode Exit fullscreen mode

This command lets you edit data in your table by updating data based on conditions specified after the WHERE keyword.

AS

SELECT column_name AS 'Alias'
FROM table_name;
Enter fullscreen mode Exit fullscreen mode

The AS keyword allows you to use a temporary alias when referring to a column or table.

COUNT

SELECT COUNT(column_name)
FROM table_name;
Enter fullscreen mode Exit fullscreen mode

Use the COUNT() function to add up the number of rows where the specified column is not NULL.

BETWEEN

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value_1 AND value_2;
Enter fullscreen mode Exit fullscreen mode

This operator filters the results to be within a specified range (numbers, text, dates, etc).


These building blocks will get you started programming with SQL, which is a great language useful and definitely worth learning in 2020. Check out the StackOverflow Developers Survey 2020, where 65k developers answered questions about the programming languages and tools they run: SQL was top three in the most popular technologies question!

Alt Text

If you're curious you can check out the rest of the results of the survey here at the URL here: https://insights.stackoverflow.com/survey/2020

Database programming languages are popular and have active developer communities, and are becoming increasingly important as organizations seek to process the thousands of terabytes of data generated each day. If you're working with databases in SQL or are planning on doing so, check out the newly-designed Arctype SQL client. It's faster and easier-to-use than many of the clients out there right now and is designed with your needs in mind as a modern developer.

Thanks for checking out my article covering these ten basic SQL commands! Let me know if you have any questions, or would like me to write a follow-up post with more intermediate SQL commands to check out.

(Originally posted here.)

Happy coding!

Discussion (1)

Collapse
n0rlant1s profile image
Bani Singh

Great sheet, Jack! Thanks for sharing