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.
Let's get right into it! Here are ten basic building blocks of SQL programming.
CREATE TABLE table_name ( column_1 datatype_1, column_2 datatype_2, column_3 datatype_3 );
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 table_name ADD column_name datatype;
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 FROM table_name WHERE some_condition = some_value;
This command can delete data from your table based on conditions specified with the WHERE keyword.
DROP TABLE table_name;
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;
The ALTER TABLE and DROP statement above will remove a specific column from a table.
INSERT INTO table_name (column_1, column_2, column_3) VALUES (value_1, value_2, value_3);
To add new records to your table, use the INSERT INTO command. You can use this command on one or more rows.
SELECT column_name FROM table_name;
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 table_name SET some_column = some_value WHERE some_column = some_value;
This command lets you edit data in your table by updating data based on conditions specified after the WHERE keyword.
SELECT column_name AS 'Alias' FROM table_name;
The AS keyword allows you to use a temporary alias when referring to a column or table.
SELECT COUNT(column_name) FROM table_name;
Use the COUNT() function to add up the number of rows where the specified column is not NULL.
SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value_1 AND value_2;
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!
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 (http://jackmcclelland.com/), or would like me to write a follow-up post with more intermediate SQL commands to check out. Happy coding!