Most viewed "sql" questions
Structured Query Language (SQL) is a language to query databases. Questions should include examples of table code and structure. This tag refers to the default language, not to be used for questions about the implementation of specific DBMS (such as Mysql, Postgresql, Oracle, MS SQL Server). In this case use the specific DBMS tag. Answers to questions marked with ANSI SQL should use SQL as much as possible.
Learn more…6,771 questions
Sort by count of
-
328
votes4
answers233186
viewsWhat is the difference between INNER JOIN and OUTER JOIN?
What’s the difference between INNER JOIN and OUTER JOIN? Can you give me some examples?
-
13
votes7
answers180098
viewsFormat decimal places directly in the SQL command in Firebird
I have a table ESTOQUE containing a field called QTDE, this field has 3 decimal places. What would be the command to return directly from SQL formatted with 3 houses? Because the integer values are…
-
50
votes4
answers82876
viewsNot IN or Not EXISTS which to use?
I’ve seen some answers here with the use Not In and some with the use of Not Exists to answer the same question. I don’t know the difference between the two and would like to know about: Which of…
-
7
votes1
answer76391
viewsINNER JOIN with 3 tables
I have 3 tables Tb_contratocotista: Tb_contract: Tb_stakeholder: I would like to relate the values of the table Tb_contract with Tb_stakeholder, but the way I got it only returns the result of…
-
33
votes5
answers74978
viewsHow to get the table name and attributes of a Mysql database?
How to get the names of all tables in the database Mysql? How to get the attributes (name, type, etc...) of a given table in the Mysql database?
-
6
votes3
answers71632
viewsHow to make a select in the bank not to bring repeated values?
For example have in the table the values: green, red, blue, blue, pink, pink, pink, yellow. I would need to return from the colors table only once the blue and the pink. Is there any way to do this?…
-
15
votes3
answers70810
viewsWhat are the appropriate data types for columns like address, email, phone and mobile phone for SQL database?
What kind of data are recommended to create the following fields: address, email, mobile number/phone?
-
65
votes6
answers66144
viewsType of the CPF or CNPJ field in the VARCHAR or INT database?
What type of CPF or CNPJ field in the database VARCHAR or INT? I’ve seen some posts suggesting using INT to optimize performance in the event of JOINand filters. If you have 0 the left is only…
-
11
votes5
answers64148
viewsDifference between two dates in days, hours, minutes and seconds
Problem: How to subtract between two dates and return the value in Days, Hours, Minutes and Seconds in an SQL query with SQL Server 2014? Example: Dados: DataRecebimento DataEnvio 2013-11-29…
-
50
votes5
answers60507
viewsDISTINCT and GROUP BY, what is the difference between the two statements?
DISTINCT The SELECT DISTINCT statement is used to return only values distinct (different). Within a table, a column usually contains many values duplicated; and sometimes you just want to list the…
-
9
votes1
answer58526
viewsHow do I make a SELECT in 2 or more Tables with 2 or more conditions?
I have a problem making a select with the following conditions: I want to show the data either with condition1 or with condition2 Select * tabela inner join tabela2 WHERE campo = 0 and campo2 = ''…
-
13
votes5
answers53548
viewsMysql decimal value
I am trying to insert decimal value in mysql database, wanted to put these values for example: 1.500,00 Thousand, hundred and ten What is the type of column I must declare?…
-
6
votes4
answers53244
viewsDifferent ways to count record numbers in Mysql
There’s a difference in performance if you do: //seleciona todos os itens da tabela $cmd = "SELECT * FROM produtos"; $produtos = mysql_query($cmd); //conta o total de itens $total =…
-
5
votes3
answers51716
viewsSQL Server - Check duplicate data in two simultaneous fields
DBMS: SQL Server 2014; Problem: I need to know which records are duplicated, so that it is checked in two simultaneous fields, that is, if the field "CPF" with the value '83971465842' and the field…
-
8
votes1
answer50072
viewsHow to get in SQL the last record inserted according to its date
I need to get a record from a table, where this record is what in date terms was last entered. Gender: SELECT Ped.Quantidade FROM ped WHERE data <= @data But since I have several records it…
-
9
votes6
answers49720
viewsConvert DATA dd/mm/yyyy hh:mm:ss to yyyy/mm/dd
I have a date in the following format 14/12/2015 00:00:00 and need to convert to 2015-12-14 as I can do it on SQL?
-
10
votes2
answers49607
viewsHow to format a Datetime field in Brazilian dd/MM/yyyy format?
I would like to format the fields DataAfr and DataTrm of the kind DATETIME, in the Brazilian date format dd/MM/YYYY instead of the American format yyyy-mm-dd hh:mm:ss SELECT e.NmrCnt AS [Contrato]…
-
29
votes2
answers48751
viewsWhat are Begin, Commit and Rollback transactions?
What are Begin, Commit, and Rollback Transactions? And how to use them? Example in practice will help a lot in understanding.
-
33
votes8
answers48170
viewsHow to calculate a person’s age in SQL Server?
Suppose the table Pessoa and the countryside DataNascimento. In an SQL query, what is the best way to calculate a person’s age in integer format in the T-SQL language of SQL Server?…
-
3
votes1
answer47156
viewsRun oracle Procedure
I created this protocol, but I can’t execute it. I have the following error when trying Execute: PLS-00306: incorrect number of argument types in call to 'SP_CARTAO' CREATE OR REPLACE PROCEDURE…
-
9
votes3
answers46833
viewsHow to rename the column name SQL Table
I want to rename the column name via SQL. I am trying as follows: ALTER TABLE nomes_clientes RENAME COLUMN primeiro_nome TO nome, RENAME COLUMN segundo_nome TO sobrenome; But that way it’s not…
-
6
votes2
answers43536
viewsError doing Insert - String or Binary data > would be truncated
By making this Internet: INSERT INTO t_cmo_oit1980_leitura(id_oit_let,id_oit,rx_num, dt_rx) SELECT (1), (SELECT id_oit FROM t_cmo_oit1980 WHERE id_oit = 6574), (SELECT rx_num FROM…
-
2
votes3
answers42978
viewsHow to make a select with multiple `Where field like '%'` in C#
select below even works, but it searches all fields and would like the idaluno, nomealuno and cpf were searched and ordered by idaluno SELECT…
-
8
votes5
answers42558
viewsHow to delete data from a table with dependencies in other tables
I need to delete data from a table that has dependencies on other tables. For example, so that I can delete a data from the person table first I have to delete a dependency that exists in the…
-
32
votes5
answers41630
viewsMysql Limit Equivalent in SQL Server
Someone knows how to relate the differences between Mysql and SQL Server, including using LIMIT n,n? Or if you have a link that lists the differences would be nice. Example SELECT * FROM tabela…
-
17
votes2
answers40729
viewsDifference between CROSS APPLY and OUTER APPLY?
What’s the difference between CROSS APPLY and OUTER APPLY? How do they work? In what situation can they be used? It would be possible to show some examples?
-
8
votes2
answers39212
viewsReplace in SQL - Character to keep a piece of text
I need to modify a part in the text of a table. The records of an email are [email protected] and I need to change everyone to jf.jus.br. Only the first part of the email doesn’t change (the xxx). I can…
-
2
votes4
answers38774
viewsSequential numbering of query results
I’m not really close to the database, and actually I don’t even need such functionality, but just out of curiosity, it’s possible to create in Runtime, that is, during SELECT, a sequential numbering…
-
5
votes5
answers36671
viewsHow to get the total character size of an SQL column?
I want to take the total size of characters that a column supports, example INT supports 10 characters. I tried to use DATALENGTH that does not return the number of characters, but the amount of…
-
19
votes4
answers35413
viewsHow to check events between 2 dates in Mysql?
Knowing I have a table called eventos, and this table has the columns id, titulo, inicio and fim. The columns inicio and fim are of the type timestamp, where the date and time of the start and end…
-
1
votes2
answers35301
viewsIF WITHIN A SELECT
SELECT @VL_RET_INSS = SUM(VL_RET_INSS), @VL_RET_IRRF = SUM(VL_RET_IRRF), FROM TABELA WHERE VALOR_TABELA > 0 I would like that when the value VL_RET_IRRF was less than 10 the field @VL_RET_IRRF…
-
9
votes2
answers34544
viewsHow to get column names from a table in SQL Server?
Table Languages: How to select the Column Names of this table? The result would be: "Ididiome", "Acronym", and "Language".…
-
6
votes4
answers34297
viewsTake the highest value per category by also loading the ID
The table is in the format below ID | CATEGORIA | QUANTIDADE 1 | A | 23 2 | A | 12 3 | A | 11 1 | B | 15 2 | B | 10 Wanted a query to return to me the following table ID | CATEGORIA | QUANTIDADE 1 |…
-
28
votes7
answers33990
viewsHow do SELECT all fields except a few?
It is known (and has been asked) which should be avoided SELECT * in some cases in bank queries. But let’s imagine that I have a table with 50 columns, and I want to select 40 of them. Just the fact…
-
4
votes4
answers33983
viewsHow to copy column data from one table to another table
I have a table called UC which has an email column, and I have a backup table of it. I need to copy the data (emails) from the table’s email column backup for the column email table uc, but I don’t…
-
9
votes2
answers33949
viewsHow to catch the current year on ORACLE?
I need to get the annual year on ORACLE, but I only know how to get the current date, so: SELECT SYSDATE FROM DUAL You can manipulate it to get only the Year?
-
41
votes2
answers33649
viewsWhat are SQL views? What are the advantages and disadvantages of using?
From my research I learned that views in SQL, are like virtual tables results of stored searches of frequent access. To W3S defines essentially as: virtual table. It has to be said that views are…
-
34
votes1
answer33629
viewsWhat is the difference between ISNULL and COALESCE in a survey?
I’m having second thoughts about using the ISNULL and COALESCE. Currently creating a query in SQL Server, I was left with doubt about ISNULL and COALESCE, I did some research and I was able to find…
-
4
votes3
answers33401
viewsSelect 1 record of each ID based on the last date
I have a fictitious table TB_META with the fields: COD_VENDEDOR | DAT_ATIVACAO | VAL_META Supposing I have these values in the table: 1 | 2011-01-01 | 1 1 | 2014-04-04 | 2 2 | 2012-01-01 | 3 2 |…
-
5
votes6
answers32941
viewsConvert varchar to date in SQL
You can convert dates into sweep for date. I’m having difficulties in performing searches between dates, due to this discrepancy in the database where I perform the query. When I enter my line of…
-
11
votes2
answers32556
viewsHow to remove duplicate lines efficiently?
I’m normalizing a table here and found that there are duplicate lines. The way I chose to clear these lines is through the: CREATE TABLE tabela_nova AS ( SELECT DISTINCT * FROM tabela_antiga ); The…
-
2
votes4
answers32475
viewsQuery to catch word after given character
Good morning, I need a way to get all the rest of a string after a certain character, for example, I have the following database I always need to find what’s left of the string after the last "»"…
-
19
votes3
answers32219
viewsFetch last 7 days data from current date
I’m making a BD query, and I want to receive results from the last 7 days from the current date. For that, I have a field data of type DATA (yyyy-mm-dd) in table. I am doing the query in php, in…
-
9
votes2
answers31808
viewsThe difference between COALESCE and NVL
In Oracle when we have a table that has two campuses A and B, and in selection (select) we want to show B when A is null we have two commands that present us the solution: Coalesce Select…
-
4
votes3
answers30960
viewsHow to query in Mysql with two different conditions in WHERE
I have two queries that work perfectly separately, but I needed all their records together. Would be these: SELECT b.idBanca AS idB, b.DataHora AS dataHora, b.Sala AS sala, t.idTrabalho AS idT,…
-
1
votes3
answers30209
viewsSQL higher date of a record before the given date
Next is the following case: Tb_importacao [cod_titulo,data,imp_situacao] cod_titulo data_imp situacao 1 2015-04-10 1 1 2015-04-11 2 1 2015-04-11 1 1 2015-04-13 2 1 2015-04-14 3 1 2015-04-14 4 I need…
-
2
votes2
answers29844
viewsConvert date to dd/MM/yyyy ORACLE
How to format sysdate for dd/MM/yyyy on Oracle 11g?
-
4
votes2
answers29746
viewsHow to pass data from one table to another SQL
I am doubtful to pass data from one table to another: Example: Table 1 --> Table 2 ---> delete all information from Table 1 I am creating the project in VB.NET and using SQL…
-
15
votes5
answers29661
viewsHow do I know if a column exists in an SQL Server table?
I am trying to add a new column to an SQL Server table, and I want to know if it already exists or not. I have tried something like this: IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE…
-
4
votes5
answers29127
viewsuse if in sql server
I have 3 tables in my database, CUSTOMERS, DISTRIBUTORS and EXPORT and I have a production order table where I take the customer type (Distributor, customer or export) and the ID of each of them.…