DEV Community

Deepangshi S.
Deepangshi S.

Posted on

Day 1: Getting Started with SQL - Basics | Beginners' Guide : Mastering

Introduction to SQL:

SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases. It allows users to create, read, update, and delete data efficiently using structured commands.

SQL is essential because it provides a universal method to interact with databases. It ensures data is stored, retrieved, and maintained systematically, enabling consistency, scalability, and security in managing vast amounts of information.

Overview of SQL Use Cases in Real-World Applications
  • Data Analysis: Querying and analyzing data in business intelligence tools.
  • Web Applications: Storing and retrieving user data for e-commerce, social media, etc.
  • Finance: Managing transactions, records, and reports in banking systems.
  • Healthcare: Handling patient records and hospital data securely. IoT and Sensor Data: Storing and processing large-scale sensor data from devices.
Core Database Concepts:
  • Database: A collection of organized data stored electronically, allowing easy access, management, and updating.
  • Table: A structured format in a database to store data in rows and columns, similar to a spreadsheet.
  • Row: A single record in a table, representing one entity or data entry.
  • Column: A vertical structure in a table, representing an attribute or field of the data (e.g., Name, Age).
  • Primary Key: A unique identifier for each row in a table (e.g., StudentID). It ensures no duplicate rows.
  • Foreign Key: A column (or combination of columns) that establishes a relationship between two tables by referencing the primary key in another table.
  • Unique Key: A constraint in a database table that ensures all values in the specified column(s) are distinct, preventing duplicate entries. Unlike a primary key, a table can have multiple unique keys, and they can contain NULL values (depending on the database).

Types of databases

Relational Databases

Databases that store data in tables with relationships between them, using SQL (e.g., MySQL, PostgreSQL, Microsoft SQL Server, Oracle Database).

Non-Relational Databases:

Databases that store data in formats like documents, key-value pairs, or graphs, and are more flexible (e.g., MongoDB, Cassandra, Redis).

First Steps in SQL:

How to Create a Database
Use the CREATE DATABASE command to set up a new database.
CREATE DATABASE my_database;
Enter fullscreen mode Exit fullscreen mode
This creates an empty database where you can create tables and store data.
How to Create a Table
Define the structure of your data using the CREATE TABLE command.
CREATE TABLE employees (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    age INT
);
Enter fullscreen mode Exit fullscreen mode
This creates a table named employees with columns id, name, and age.
Inserting Your First Record
Use the INSERT INTO command to add data into a table.
INSERT INTO employees (id, name, age)  
VALUES (1, 'John Doe', 50);
Enter fullscreen mode Exit fullscreen mode
This adds a new row with the specified values to the employees table.
Simple Query to Fetch Data
Use the SELECT statement to retrieve data from a table.
SELECT * FROM employees;
Enter fullscreen mode Exit fullscreen mode
This retrieves all rows and columns from the employees table.

Challenge for You!
Create your own table (e.g., books, employees, or anything you like).
Insert at least 3 records into your table.
Use SELECT to display the data.

In the next post, we will dive deeper into SQL commands, including DDL, DML, and more!

Top comments (0)