SQL Articles

Page 2 of 12

How to Create a Table With Multiple Foreign Keys in SQL?

SQL
Yashika Bhatia
Yashika Bhatia
Updated on 17-Mar-2025 299 Views

A foreign key is a field (or collection of fields) in one table that refers to the primary key in another table. The table containing the foreign key is called the child table, and the table containing the primary key is called the parent table. A foreign key ensures that the value in the foreign key column must match a value in the primary key column of the referenced table, maintaining referential integrity between the two tables. Characteristics of a Foreign Key A foreign key creates a relationship between two tables. ...

Read More

SQL Query to Get Column Names From a Table

SQL
Rohit Kumar Dwivedi
Rohit Kumar Dwivedi
Updated on 28-Jan-2025 355 Views

In this article, we are going to learn how to get column names from a table using an SQL query. If you don't know what SQL is, keep reading because everything is covered from scratch. SQL stands for Structured Query Language, which is used to interact with databases. Basically, queries are used to perform CRUD operations on databases. Steps to Retrieve Column Names Here is the step-by-step explanation of getting columns from a table. Step 1: Check Database First, check the database where the table exists. If the database does not exist, run the following query to create it. CREATE ...

Read More

How to Write a SQL Query For a Specific Date Range and Date Time?

SQL
guru
guru
Updated on 27-Jan-2025 514 Views

Filtering data by date or datetime is a common requirement when working with SQL queries. Whether you're retrieving records for a specific date, a range of dates or precise timestamps SQL provides robust tools to handle such scenarios. Understanding Date and DateTime Data Types The SQL databases support various data types for storing the date and time information: DATE: Stores only the date (e.g. 2025-01-07). DATETIME: The Stores both date and time (e.g. 2025-01-07 14:30:00). TIMESTAMP: The Stores date and time with time zone information in some ...

Read More

Joining three or more tables in SQL

SQL
guru
guru
Updated on 27-Jan-2025 337 Views

In SQL, joining tables is an essential operation that combines data from multiple sources. While joining two tables is straightforward many real-world scenarios require joining three or more tables to retrieve comprehensive information. This article explains how to join three or more tables, complete with examples. Understanding Joins SQL joins are used to combine rows from two or more tables based on a related column. The Common types of joins include: INNER JOIN: Returns records that have matching values in both tables. ...

Read More

Query to find 2nd largest value in a column in Table

SQL
Rohit Kumar Dwivedi
Rohit Kumar Dwivedi
Updated on 24-Jan-2025 751 Views

In this article, we will learn how to find the 2nd largest value in a column of a table. It’s a pretty common task when working with databases, especially when you need to analyze data. We will explore efficient methods to find the second-largest value using SQL queries, with step-by-step explanations and examples for better understanding. Steps to Retrieve 2nd Largest Value in a Column Here is the step-by-step explanation of getting 2nd largest value in a column in a table. Step 1: Check Database First, check the database where the table exists. If the database does not exist, run the ...

Read More

Difference between stored procedure and triggers in SQL

Venu Madhavi
Venu Madhavi
Updated on 22-Jan-2025 21K+ Views

Stored Procedures Stored Procedures is a group of pre-compiled SQL statements that is stored in a database and can be reused anytime. It allows you to do many things in one command. So doing several activities in a database will be easier and quicker than before. A stored procedure can be called from SQL commands as well as applications like Python, Java, or PHP. Syntax Following is the syntax to create a stored procedure − CREATE PROCEDURE procedure_name AS BEGIN -- SQL statements END; Example Let us create a stored procedure where it ...

Read More

SQL Query to Find the Highest Salary of Each Department

SQL
Nishu Kumari
Nishu Kumari
Updated on 17-Jan-2025 267 Views

SQL (Structured Query Language) is a programming language used to manage and interact with databases. In this case, the goal is to find the highest salary in each department from a table that contains employee data, including their salaries and the departments they work in. We'll write an SQL query to find the highest salary for each department, which is useful for analyzing salary trends, setting pay standards, and making informed business decisions. Finding the Highest Salary of Each Department To find the highest salary in each department, use SQL's GROUP BY and MAX() functions. The GROUP BY ...

Read More

SQL Query for Matching Multiple Values in the Same Column

SQL
Nishu Kumari
Nishu Kumari
Updated on 17-Jan-2025 588 Views

To query data in SQL where you need to match multiple values within the same column, you can use operators like IN and OR. For example, how would you find employees in the "Sales" or "Marketing" departments, or list those with job titles like "Manager" or "Salesperson"? These are common scenarios when filtering data based on multiple conditions in SQL: The IN operator allows you to match values from a list, while OR helps combine multiple conditions efficiently. In this article, we'll show you how to write SQL queries to match multiple values within the same column, making it easier ...

Read More

How to Select Data Between Two Dates and Times in SQL Server?

SQL
Nishu Kumari
Nishu Kumari
Updated on 17-Jan-2025 304 Views

Sometimes, you need to get data from SQL Server that falls between two specific dates or times, like finding all orders placed within a week or all activities that happened during a certain time frame. Writing the right query can be challenging if you're not familiar with how to filter data by date and time. In this article, we will show you how to easily select data between two dates and times in SQL Server using simple queries. Steps to Write Queries for Selecting Data Between Dates and Times As we're going to write queries to filter data ...

Read More

How to Select All Records from One Table That Do Not Exist in Another Table in SQL?

SQL
TEJA KOTAMRAJU
TEJA KOTAMRAJU
Updated on 17-Jan-2025 317 Views

In SQL, there are several ways to achieve this, including using LEFT JOIN, NOT EXISTS, and NOT IN. This article will explore these methods in detail, providing examples and guidance on when to use each approach. Using LEFT JOIN with IS NULL Using NOT EXISTS Using NOT IN 1. Using LEFT JOIN with IS NULL The LEFT JOIN operation returns all records from the left table (the first table) and the matching records from the right table (the second table). When no match is found, ...

Read More
Showing 11–20 of 114 articles
« Prev 1 2 3 4 5 12 Next »
Advertisements